package com.hong.job_management;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import static com.hong.job_management.MainActivity.chinese;
import static com.hong.job_management.MainActivity.chinese_finish;
import static com.hong.job_management.MainActivity.biology;
import static com.hong.job_management.MainActivity.biology_finish;
import static com.hong.job_management.MainActivity.chemistry;
import static com.hong.job_management.MainActivity.chemistry_finish;
import static com.hong.job_management.MainActivity.english;
import static com.hong.job_management.MainActivity.english_finish;
import static com.hong.job_management.MainActivity.geography;
import static com.hong.job_management.MainActivity.geography_finish;
import static com.hong.job_management.MainActivity.history;
import static com.hong.job_management.MainActivity.history_finish;
import static com.hong.job_management.MainActivity.math;
import static com.hong.job_management.MainActivity.math_finish;
import static com.hong.job_management.MainActivity.other;
import static com.hong.job_management.MainActivity.other_finish;
import static com.hong.job_management.MainActivity.physics;
import static com.hong.job_management.MainActivity.physics_finish;
import static com.hong.job_management.MainActivity.politics;
import static com.hong.job_management.MainActivity.politics_finish;
import static com.hong.job_management.MainActivity.sum;
import static com.hong.job_management.MainActivity.sum_finish;


public class RegisterActivity extends AppCompatActivity {
        private Button bt_register;
        private Spinner subject;
        private String subject_select;
        private Spinner rank;
        private String rank_select;
        private DatePicker time;
        private String time_select;
        private EditText name;
        private String name_editext;
        private int isfinish=0;
        private RegisterActivity ScreenUtil;

        MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bt_register=(Button)findViewById(R.id.btn_homework_dj);
        subject=(Spinner)findViewById(R.id.subject);
        rank=(Spinner)findViewById(R.id.rank);
        time=(DatePicker)findViewById((R.id.time));
        name=(EditText)findViewById(R.id.name);
        myHelper=new MyHelper(this);

        rank.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                Toast.makeText(RegisterActivity.this, "您选择了 ：" +parent.getItemAtPosition(position).toString() , Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        subject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position!=0)
                Toast.makeText(RegisterActivity.this, "您选择了 ：" +parent.getItemAtPosition(position).toString() , Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScreenUtil.closeSoftInput(RegisterActivity.this, v);//关闭输入法

                SQLiteDatabase db;
                ContentValues values;
                name_editext=name.getText().toString().trim();
                subject_select=subject.getSelectedItem().toString();
                rank_select=rank.getSelectedItem().toString();
                int year=time.getYear();
                int month=time.getMonth()+1;
                int day=time.getDayOfMonth();
                time_select=(String.valueOf(year)+"."+String.format("%02d",month)+"."+String.format("%02d",day));
                if(name_editext.length()==0||subject_select.equals("科目")||rank_select.equals("难度"))
                {
//                    db=myHelper.getWritableDatabase();
//                    db.delete("homework",null,null);
//                    db.close();
                    if(subject_select.equals("科目"))
                        Toast.makeText(RegisterActivity.this, "请选择作业科目", Toast.LENGTH_SHORT).show();
                    else if(name_editext.length()==0)
                        Toast.makeText(RegisterActivity.this, "请填写作业名称", Toast.LENGTH_SHORT).show();
                    else if (rank_select.equals("难度"))
                        Toast.makeText(RegisterActivity.this, "请选择作业难度", Toast.LENGTH_SHORT).show();
                }else{
                    name_editext=name.getText().toString();
                    switch (subject_select)
                    {
                        case "语文":
                            if(rank_select.equals("简单"))
                            {
                                chinese=chinese+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                chinese=chinese+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                chinese=chinese+3;
                                sum=sum+3;
                            }
                            Toast.makeText(RegisterActivity.this, "作业登记成功！", Toast.LENGTH_SHORT).show();
                            break;
                        case "数学":
                            if(rank_select.equals("简单"))
                            {
                                math=math+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                math=math+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                math=math+3;
                                sum=sum+3;
                            }
                            break;
                        case "英语":
                            if(rank_select.equals("简单"))
                            {
                                english=english+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                english=english+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                english=english+3;
                                sum=sum+3;
                            }
                            break;
                        case "生物":
                            if(rank_select.equals("简单"))
                            {
                                biology=biology+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                biology=biology+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                biology=biology+3;
                                sum=sum+3;
                            }
                            break;
                        case "物理":
                            if(rank_select.equals("简单"))
                            {
                                physics=physics+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                physics=physics+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                physics=physics+3;
                                sum=sum+3;
                            }
                            break;
                        case "化学":
                            if(rank_select.equals("简单"))
                            {
                                chemistry=chemistry+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                chemistry=chemistry+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                chemistry=chemistry+3;
                                sum=sum+3;
                            }
                            break;
                        case "历史":
                            if(rank_select.equals("简单"))
                            {
                                history=history+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                history=history+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                history=history+3;
                                sum=sum+3;
                            }
                            break;
                        case "政治":
                            if(rank_select.equals("简单"))
                            {
                                politics=politics+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                politics=politics+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                politics=politics+3;
                                sum=sum+3;
                            }
                            break;
                        case "地理":
                            if(rank_select.equals("简单"))
                            {
                                geography=geography+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                geography=geography+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                geography=geography+3;
                                sum=sum+3;
                            }
                            break;
                        case "其他":
                            if(rank_select.equals("简单"))
                            {
                                other=other+1;
                                sum=sum+1;
                            }
                            else if(rank_select.equals("中等"))
                            {
                                other=other+2;
                                sum=sum+2;
                            }
                            else if(rank_select.equals("困难"))
                            {
                                other=other+3;
                                sum=sum+3;
                            }
                            break;
                    }

                    SharedPreferences myprefs=getSharedPreferences("homework_jd",MODE_PRIVATE);
                    SharedPreferences.Editor editor=myprefs.edit();
                    editor.putInt("chinese",chinese);
                    editor.putInt("math",math);
                    editor.putInt("english",english);
                    editor.putInt("biology",biology);
                    editor.putInt("chemistry",chemistry);
                    editor.putInt("physics",physics);
                    editor.putInt("history",history);
                    editor.putInt("politics",politics);
                    editor.putInt("geography",geography);
                    editor.putInt("other",other);
                    editor.putInt("sum",sum);
                    editor.commit();
                    editor.apply();

                    //储存后清零
                    name.setText("");
                    rank.setSelection(0);
                    subject.setSelection(0);

                    values=new ContentValues();
                    values.put("subject",subject_select);
                    values.put("name",name_editext);
                    values.put("time",time_select);
                    values.put("rank",rank_select);
                    //values.put("isfinish",isfinish);
                    db=myHelper.getWritableDatabase();
                    long insert_pd=db.insert("homework",null,values);
                    db.close();
                    if(insert_pd!=-1)//插入失败为-1
                        Toast.makeText(RegisterActivity.this, "作业登记成功！", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(RegisterActivity.this, "作业登记失败！", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    public boolean onTouchEvent(MotionEvent event) {
        closeKeyBoard();
        return super.onTouchEvent(event);
    }

    public void closeKeyBoard() {
        if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
            View v = getCurrentFocus();
            ScreenUtil.closeSoftInput(this, v);
        }
    }

    public static void closeSoftInput(Context context, View v) {
        if (v != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
    
}