package com.hong.job_management;

import androidx.annotation.ContentView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.Telephony;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity  {
    private static final String urlPath = "http://192.168.56.1:8080/backgroud.json";
    private RelativeLayout main_layout;
    public List<Homework> homework = new ArrayList<Homework>();
    private List<String> url_backgroud = new ArrayList<>();
    public myAdapter_Homework adapter_homework;
    public SwipeMenuListView listView;
    MyHelper myHelper=new MyHelper(MainActivity.this);
    private ImageView change;
    private int background_index=1;
    private SensorManager sensorManager;
    private Sensor tempertureSensor;
    private TextView temp;

    public static int chinese;
    public static int chinese_finish;
    public static int math;
    public static int math_finish;
    public static int english;
    public static int english_finish;
    public static int biology;
    public static int biology_finish;
    public static int chemistry;
    public static int chemistry_finish;
    public static int physics;
    public static int physics_finish;
    public static int history;
    public static int history_finish;
    public static int politics;
    public static int politics_finish;
    public static int geography;
    public static int geography_finish;
    public static int other;
    public static int other_finish;
    public static int sum;
    public static int sum_finish;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//除去标题（得放在setContentView之前）
        if (getSupportActionBar() != null) {           //除去标题
            getSupportActionBar().hide();             //除去标题
        }
        setContentView(R.layout.activity_main);

        change=findViewById(R.id.change_background);
        temp=findViewById(R.id.temp);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        tempertureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
        if (tempertureSensor == null) {
            Toast.makeText(MainActivity.this, "你的设备不支持测温功能", Toast.LENGTH_SHORT).show();
        }else{
            sensorManager.registerListener(sensorEventListener, tempertureSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }

        SharedPreferences myprefs=getSharedPreferences("homework_jd",MODE_PRIVATE);
        chinese=myprefs.getInt("chinese",0);
        chinese_finish=myprefs.getInt("chinese_finish",0);
        math=myprefs.getInt("math",0);
        math_finish=myprefs.getInt("math_finish",0);
        english=myprefs.getInt("english",0);
        english_finish=myprefs.getInt("english_finish",0);
        biology=myprefs.getInt("biology",0);
        biology_finish=myprefs.getInt("biology_finish",0);
        chemistry=myprefs.getInt("chemistry",0);
        chemistry_finish=myprefs.getInt("chemistry_finish",0);
        physics=myprefs.getInt("physics",0);
        physics_finish=myprefs.getInt("physics_finish",0);
        history=myprefs.getInt("history",0);
        history_finish=myprefs.getInt("history_finish",0);
        politics=myprefs.getInt("politics",0);
        politics_finish=myprefs.getInt("politics_finish",0);
        geography=myprefs.getInt("geography",0);
        geography_finish=myprefs.getInt("geography_finish",0);
        other=myprefs.getInt("other",0);
        other_finish=myprefs.getInt("other_finish",0);
        sum=myprefs.getInt("sum",0);
        sum_finish=myprefs.getInt("sum_finish",0);

        //网络设置 会秒退
        StrictMode.setThreadPolicy(new
                StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(
                new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(background_index==0)background_index=1;
                else background_index=0;
                Bitmap bitmap = null;
                try {
                    url_backgroud = JsonParse.getList(urlPath);
                    bitmap = getBitmap(url_backgroud.get(background_index));
                    BitmapDrawable bd=new BitmapDrawable(bitmap);
                    main_layout=(RelativeLayout)findViewById(R.id.main_layout);
                    main_layout.setBackground(bd);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        Button btn_dj = (Button) findViewById(R.id.btn_dj);
        //View view_dj = findViewById(R.id.btn_dj);
        Button btn_jd = (Button) findViewById(R.id.btn_jd);
        //View view_jd = findViewById(R.id.btn_jd);
        Button btn_tj = (Button) findViewById(R.id.btn_tj);
        //View view_tj = findViewById(R.id.btn_tj);
        final MainActivity thisActivity = this;
        adapter_homework = new myAdapter_Homework(MainActivity.this, homework);
        listView = (SwipeMenuListView) findViewById(R.id.homework_list);
        listView.setAdapter(adapter_homework);

        btn_dj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(thisActivity, RegisterActivity.class);
                thisActivity.startActivity(intent);
            }
        });
        btn_jd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(thisActivity, ScheduleActivity.class);
                thisActivity.startActivity(intent);
            }
        });
        btn_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(thisActivity, StatisticsActivity.class);
                thisActivity.startActivity(intent);
            }
        });

        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                int id =menu.getMenuItem(index).getId();
                SQLiteDatabase db=myHelper.getWritableDatabase();
                switch (id) {
                    case 0:
                    case 1:
                        // is_finish
                        // no_finish
                        is_finish(position);
                        createMenue();
                        break;
                    case 2:
                        // delete
                        delete(position);
                        break;
                }
                createMenue();
                return false;
            }
        });

        //set SwipeListener
        listView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }
            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });
    }

    public Bitmap getBitmap(String path) throws IOException {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

        //温度传感器
    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            temp.setText(values[0]+"℃");
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        sensorManager.unregisterListener(sensorEventListener);
    }
    //温度以上

    @Override
    protected void onStart() {
        super.onStart();
        initHomework();
        listView.setAdapter(adapter_homework);
        createMenue();
    }

    private void createMenue (){
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                switch (menu.getViewType())
                {
                    case 0: //未完成作业 用已经完成按钮
                        createMenu1(menu);
                        break;
                    case  1://已经完成作业 用未完成按钮
                        createMenu2(menu);
                        break;
                }
            }
        };
        listView.setMenuCreator(creator);
    }

    private void createMenu1 (SwipeMenu menu) {
        // create "no_finish" item
        SwipeMenuItem no_finishItem = new SwipeMenuItem(getApplicationContext());
        no_finishItem.setId(0);
        // set item background
        no_finishItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
        // set item width
        no_finishItem.setWidth(dp2px(90));
        // set item title
        no_finishItem.setTitle("已完成");
        // set item title fontsize
        no_finishItem.setTitleSize(18);
        // set item title font color
        no_finishItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(no_finishItem);

        // create "delete" item
        SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
        deleteItem.setId(2);
        // set item background
        deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
        // set item width
        deleteItem.setWidth(dp2px(90));
        // set a icon
        deleteItem.setTitle("删除");
        deleteItem.setTitleSize(18);
        deleteItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(deleteItem);
    }

    private void createMenu2 (SwipeMenu menu) {
        // create "finish" item
        SwipeMenuItem finishItem = new SwipeMenuItem(getApplicationContext());
        finishItem.setId(1);
        // set item background
        finishItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
        // set item width
        finishItem.setWidth(dp2px(90));
        // set item title
        finishItem.setTitle("未完成");
        // set item title fontsize
        finishItem.setTitleSize(18);
        // set item title font color
        finishItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(finishItem);

        // create "delete" item
        SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
        deleteItem.setId(2);
        // set item background
        deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
        // set item width
        deleteItem.setWidth(dp2px(90));
        // set a icon
        deleteItem.setTitle("删除");
        deleteItem.setTitleSize(18);
        deleteItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(deleteItem);
    }

    private void is_finish ( int position){
        Homework h = homework.get(position);
        int pd=h.isfinish;
        SQLiteDatabase db =myHelper.getWritableDatabase();
        ContentValues values= new ContentValues();

        Cursor cursor = db.query("homework",null,null,null,null,null,null);
        cursor.moveToPosition(position);
        int itemid=cursor.getInt(cursor.getColumnIndex("_id"));

        if (h != null)
        {
            if(pd==1)
            {
                values.put("isfinish",0);
                db.update("homework",values,"_id=?",new String[]{itemid+""});
                homework.get(position).isfinish =0;
                Toast.makeText(MainActivity.this, "未完成 ： " + h.getHomework_name(), Toast.LENGTH_SHORT).show();
                switch (h.getHomework_subject())
                {
                    case "语文":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            chinese_finish=chinese_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            chinese_finish=chinese_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            chinese_finish=chinese_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "数学":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            math_finish=math_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            math_finish=math_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            math_finish=math_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "英语":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            english_finish=english_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            english_finish=english_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            english_finish=english_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "生物":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            biology_finish=biology_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            biology_finish=biology_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            biology_finish=biology_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "物理":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            physics_finish=physics_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            physics_finish=physics_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            physics_finish=physics_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "化学":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            chemistry_finish=chemistry_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            chemistry_finish=chemistry_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            chemistry_finish=chemistry_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "历史":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            history_finish=history_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            history_finish=history_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            history_finish=history_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "政治":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            politics_finish=politics_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            politics_finish=politics_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            politics_finish=politics_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "地理":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            geography_finish=geography_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            geography_finish=geography_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            geography_finish=geography_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                    case "其他":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            other_finish=other_finish-1;
                            sum_finish=sum_finish-1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            other_finish=other_finish-2;
                            sum_finish=sum_finish-2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            other_finish=other_finish-3;
                            sum_finish=sum_finish-3;
                        }
                        break;
                }
            }else {
                values.put("isfinish",1);
                db.update("homework",values,"_id=?",new String[]{itemid+""});
                homework.get(position).isfinish = 1;
                Toast.makeText(MainActivity.this, "已完成 ： " + h.getHomework_name(), Toast.LENGTH_SHORT).show();
                switch (h.getHomework_subject())
                {
                    case "语文":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            chinese_finish=chinese_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            chinese_finish=chinese_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            chinese_finish=chinese_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "数学":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            math_finish=math_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            math_finish=math_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            math_finish=math_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "英语":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            english_finish=english_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            english_finish=english_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            english_finish=english_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "生物":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            biology_finish=biology_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            biology_finish=biology_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            biology_finish=biology_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "物理":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            physics_finish=physics_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            physics_finish=physics_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            physics_finish=physics_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "化学":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            chemistry_finish=chemistry_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            chemistry_finish=chemistry_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            chemistry_finish=chemistry_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "历史":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            history_finish=history_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            history_finish=history_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            history_finish=history_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "政治":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            politics_finish=politics_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            politics_finish=politics_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            politics_finish=politics_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "地理":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            geography_finish=geography_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            geography_finish=geography_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            geography_finish=geography_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                    case "其他":
                        if(h.getHomework_rank().equals("简单"))
                        {
                            other_finish=other_finish+1;
                            sum_finish=sum_finish+1;
                        }
                        else if(h.getHomework_rank().equals("中等"))
                        {
                            other_finish=other_finish+2;
                            sum_finish=sum_finish+2;
                        }
                        else if(h.getHomework_rank().equals("困难"))
                        {
                            other_finish=other_finish+3;
                            sum_finish=sum_finish+3;
                        }
                        break;
                }
            }
            cursor.close();
            db.close();
            adapter_homework.notifyDataSetChanged();
            SharedPreferences myprefs=getSharedPreferences("homework_jd",MODE_PRIVATE);
            SharedPreferences.Editor editor=myprefs.edit();
            editor.putInt("chinese_finish",chinese_finish);
            editor.putInt("math_finish",math_finish);
            editor.putInt("english_finish",english_finish);
            editor.putInt("biology_finish",biology_finish);
            editor.putInt("chemistry_finish",chemistry_finish);
            editor.putInt("physics_finish",physics_finish);
            editor.putInt("history_finish",history_finish);
            editor.putInt("politics_finish",politics_finish);
            editor.putInt("geography_finish",geography_finish);
            editor.putInt("other_finish",other_finish);
            editor.putInt("sum_finish",sum_finish);
            editor.commit();
            editor.apply();
        }
    }

    private void delete(int position) {
        SQLiteDatabase db=myHelper.getWritableDatabase();
        Homework h = homework.get(position);
        Cursor cursor = db.query("homework",null,null,null,null,null,null);
        cursor.moveToPosition(position);
        int itemid=cursor.getInt(cursor.getColumnIndex("_id"));
        if (h != null) {
            int delete_pd=db.delete("homework","_id=?",new String []{itemid+""});
            if(delete_pd==1) //删除成功为1 不成功为0
            {
                homework.remove(h);
                adapter_homework.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "删除 ： " + h.getHomework_name(), Toast.LENGTH_SHORT).show();
                int pd=h.isfinish;
                    if(pd==1)
                    {
                        switch (h.getHomework_subject())
                        {
                            case "语文":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    chinese=chinese-1;
                                    chinese_finish=chinese_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    chinese=chinese-2;
                                    chinese_finish=chinese_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    chinese=chinese-3;
                                    chinese_finish=chinese_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "数学":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    math=math-1;
                                    math_finish=math_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    math=math-2;
                                    math_finish=math_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    math=math-3;
                                    math_finish=math_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "英语":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    english=english-1;
                                    english_finish=english_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    english=english-2;
                                    english_finish=english_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    english=english-3;
                                    english_finish=english_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "生物":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    biology=biology-1;
                                    biology_finish=biology_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    biology=biology-2;
                                    biology_finish=biology_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    biology=biology-3;
                                    biology_finish=biology_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "物理":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    physics=physics-1;
                                    physics_finish=physics_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    physics=physics-2;
                                    physics_finish=physics_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    physics=physics-3;
                                    physics_finish=physics_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "化学":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    chemistry=chemistry-1;
                                    chemistry_finish=chemistry_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    chemistry=chemistry-2;
                                    chemistry_finish=chemistry_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    chemistry=chemistry-3;
                                    chemistry_finish=chemistry_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "历史":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    history=history-1;
                                    history_finish=history_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    history=history-2;
                                    history_finish=history_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    history=history-3;
                                    history_finish=history_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "政治":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    politics=politics-1;
                                    politics_finish=politics_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    politics=politics-2;
                                    politics_finish=politics_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    politics=politics-3;
                                    politics_finish=politics_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "地理":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    geography=geography-1;
                                    geography_finish=geography_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    geography=geography-2;
                                    geography_finish=geography_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    geography=geography-3;
                                    geography_finish=geography_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                            case "其他":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    other=other-1;
                                    other_finish=other_finish-1;
                                    sum=sum-1;
                                    sum_finish=sum_finish-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    other=other-2;
                                    other_finish=other_finish-2;
                                    sum=sum-2;
                                    sum_finish=sum_finish-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    other=other-3;
                                    other_finish=other_finish-3;
                                    sum=sum-3;
                                    sum_finish=sum_finish-3;
                                }
                                break;
                        }
                    }else {
                        switch (h.getHomework_subject())
                        {
                            case "语文":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    chinese=chinese-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    chinese=chinese-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    chinese=chinese-3;
                                    sum=sum-3;
                                }
                                break;
                            case "数学":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    math=math-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    math=math-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    math=math-3;
                                    sum=sum-3;
                                }
                                break;
                            case "英语":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    english=english-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    english=english-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    english=english-3;
                                    sum=sum-3;
                                }
                                break;
                            case "生物":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    biology=biology-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    biology=biology-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    biology=biology-+3;
                                    sum=sum-3;
                                }
                                break;
                            case "物理":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    physics=physics-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    physics=physics-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    physics=physics-3;
                                    sum=sum-3;
                                }
                                break;
                            case "化学":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    chemistry=chemistry-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    chemistry=chemistry-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    chemistry=chemistry-3;
                                    sum=sum-3;
                                }
                                break;
                            case "历史":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    history=history-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    history=history-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    history=history-3;
                                    sum=sum-3;
                                }
                                break;
                            case "政治":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    politics=politics-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    politics=politics-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    politics=politics-3;
                                    sum=sum-3;
                                }
                                break;
                            case "地理":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    geography=geography-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    geography=geography-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().equals("困难"))
                                {
                                    geography=geography-3;
                                    sum=sum-3;
                                }
                                break;
                            case "其他":
                                if(h.getHomework_rank().equals("简单"))
                                {
                                    other=other-1;
                                    sum=sum-1;
                                }
                                else if(h.getHomework_rank().equals("中等"))
                                {
                                    other=other-2;
                                    sum=sum-2;
                                }
                                else if(h.getHomework_rank().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("chinese_finish",chinese_finish);
                editor.putInt("math",math);
                editor.putInt("math_finish",math_finish);
                editor.putInt("english",english);
                editor.putInt("english_finish",english_finish);
                editor.putInt("biology",biology);
                editor.putInt("biology_finish",biology_finish);
                editor.putInt("chemistry",chemistry);
                editor.putInt("chemistry_finish",chemistry_finish);
                editor.putInt("physics",physics);
                editor.putInt("physics_finish",physics_finish);
                editor.putInt("history",history);
                editor.putInt("history_finish",history_finish);
                editor.putInt("politics",politics);
                editor.putInt("politics_finish",politics_finish);
                editor.putInt("geography",geography);
                editor.putInt("geography_finish",geography_finish);
                editor.putInt("other",other);
                editor.putInt("other_finish",other_finish);
                editor.putInt("sum",sum);
                editor.putInt("sum_finish",sum_finish);
                editor.commit();
                editor.apply();
            }else
            {
                Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
            }
            cursor.close();
            db.close();
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    public void initHomework()
        {
            String name;
            String rank;
            String time;
            String subject;
            int isfinish;
            SQLiteDatabase db;
            db=myHelper.getReadableDatabase();
            Cursor cursor=db.query("homework",null,null,null,null,null,null,null);
            homework.clear();
            if(cursor.getCount()==0){
                Toast.makeText(MainActivity.this,"请添加作业",Toast.LENGTH_SHORT).show();
            }else{
                while(cursor.moveToNext())
                {
                    name=cursor.getString(cursor.getColumnIndex("name"));
                    subject=cursor.getString(cursor.getColumnIndex("subject"));
                    rank=cursor.getString(cursor.getColumnIndex("rank"));
                    time=cursor.getString(cursor.getColumnIndex("time"));
                    isfinish=cursor.getInt(cursor.getColumnIndex("isfinish"));
                    Homework h=new Homework(name,subject,rank,time,isfinish);
                    homework.add(h);
                }
                cursor.close();
                db.close();
            }
        }
    }