package com.example.map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.map.database.ClassDBHelper;
import com.example.map.enity.Class_demo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ClassTable extends AppCompatActivity implements View.OnTouchListener {

    private  static final String DB_NAME="class.db";
    private  static final String TABLE_NAME="class_table";
    private  static final int DB_VERSION=1;
    private ClassDBHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_table);

        ClassDBHelper dbHelper=new ClassDBHelper(this,DB_NAME,null,DB_VERSION);

        framework();
        applyDraw(dbHelper);

    }
    protected void onStart() {
        super.onStart();
        // 获得数据库帮助器的实例
        mHelper = ClassDBHelper.getInstance(this);
        // 打开数据库帮助器的读写连接
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }
    public GridLayout LayoutColumn(int i) {
        //di->周一
        GridLayout gridLayout = findViewById(R.id.d1);
        switch (i) {
            case 1: {
                gridLayout = findViewById(R.id.d1);
                break;
            }
            case 2: {
                gridLayout = findViewById(R.id.d2);
                break;
            }
            case 3: {
                gridLayout = findViewById(R.id.d3);
                break;
            }
            case 4: {
                gridLayout = findViewById(R.id.d4);
                break;
            }
            case 5: {
                gridLayout = findViewById(R.id.d5);
                break;
            }
            case 6: {
                gridLayout = findViewById(R.id.d6);
                break;
            }
            case 7: {
                gridLayout = findViewById(R.id.d7);
                break;
            }
        }
        return gridLayout;
    }

    public void framework() {
        GridLayout gridLayout;
        int id = 1;
        for (int i = 1; i < 8; i++) {
            gridLayout = LayoutColumn(i);
            for (int j = 1; j < 10; j +=2) {
                TextView textView1 = new TextView(this);
                textView1.setId(id++);
                textView1.setText("");
                textView1.setMaxLines(5);
                textView1.setEllipsize(TextUtils.TruncateAt.END);
                textView1.setBackgroundColor(Color.parseColor("#F0FFFF"));
                textView1.setGravity(Gravity.CENTER);
                GridLayout.LayoutParams params1 = new GridLayout.LayoutParams();
                params1.rowSpec = GridLayout.spec( j, 2,1);
                params1.setMargins(5,10,5,10);
                params1.width = GridLayout.LayoutParams.MATCH_PARENT;
                params1.height = 0;
                gridLayout.addView(textView1, params1);
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    public void applyDraw(ClassDBHelper DBHelper) {
        List<Class_demo> classes = query(DBHelper);
        for (Class_demo aClass : classes) {
            //第几节课
            int i = Integer.parseInt(aClass.c_time.charAt(0) + "");
//            int i=1;
            //星期几
            int j = utils.getDay(aClass.getC_day());
            //获取此课程对应TextView的id
            TextView Class = findViewById((j - 1) * 5 + ((i - 1)/2 + 1));

            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
            if (aClass.getC_day().equals(simpleDateFormat.format(date).toString())) {
                Class.setBackgroundColor(Color.rgb(28, 217, 204));
            }
            //课程表信息映射出来
            int louceng=(int)(Math.random()*10+1);
            int jiaoshihao=(int)(Math.random()*400+1);
            String jiaoshi="S"+louceng+"-"+jiaoshihao;
            String xinxi="课程："+aClass.getC_subject()+'\n'+ "教师："+aClass.getC_teacher()+'\n';
            Class.setText(xinxi);
            //触碰此课程框触发
            Class.setOnTouchListener(ClassTable.this);
        }
    }
    public List<Class_demo> query(ClassDBHelper dbHelper) {

        List<Class_demo> classes = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor1 = db.query(TABLE_NAME, null, null, null, null, null, null);
//        cursor1.moveToFirst();
        while (cursor1.moveToNext()) {
            Cursor cursor=db.query(TABLE_NAME,null,null,null,null,null,null);
            @SuppressLint("Range") String subject=cursor1.getString(cursor.getColumnIndex("c_subject"));
            @SuppressLint("Range") String time=cursor1.getString(cursor.getColumnIndex("c_time"));
            @SuppressLint("Range") String day=cursor1.getString(cursor.getColumnIndex("c_day"));
            @SuppressLint("Range") String teacher=cursor1.getString(cursor.getColumnIndex("c_teacher"));
            classes.add(new Class_demo(subject,time,day,teacher));
        }
        db.close();
        return classes;
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem menuItem=menu.findItem(R.id.action_menu);
        MenuItem close=menu.findItem(R.id.action_close);
        MenuItem delete_class=menu.findItem(R.id.action_delete);
        menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                Intent intent = new Intent();
                intent.setClass(ClassTable.this, AddClasses.class);
                startActivity(intent);
                return true;
            }
        });
        close.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent();
                intent.setClass(ClassTable.this, MainActivity.class);
                startActivity(intent);
                return true;
            }
        });

        delete_class.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent();
                intent.setClass(ClassTable.this, DeleteClasses.class);
                startActivity(intent);
                return true;
            }
        });

        return  super.onCreateOptionsMenu(menu);
    }

    /**
     * Called when a touch event is dispatched to a view. This allows listeners to
     * get a chance to respond before the target view.
     *
     * @param v     The view the touch event has been dispatched to.
     * @param event The MotionEvent object containing full information about
     *              the event.
     * @return True if the listener has consumed the event, false otherwise.
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
}