package com.rehome.meetingbook;

import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;


import com.rehome.meetingbook.adapter.GridViewAdapter;
import com.rehome.meetingbook.base.BaseActivity2;
import com.rehome.meetingbook.bean.GridViewBean;
import com.rehome.meetingbook.databinding.ActivityMainBinding;
import com.rehome.meetingbook.ui.activity.LoginActivity;
import com.rehome.meetingbook.utils.ControllerActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity2<ActivityMainBinding> {

    GridView gv;
    private List<GridViewBean> beanList = new ArrayList<>();
    private GridViewAdapter adapter;

    private List<String> dialogDatas;
    private List<String> dialogDatas2;
    DownloadManager DM, mDownloadManager;
    private long downloadId;
    private boolean isTask = false;//是否有任务
    private long exitTime = 0;
//    private String[] str = {"考试预约", "预约记录", "安健环巡查", "综合巡查", "行为安全观察", "工单录入", "巡视抄表", "定期工作", "定期工作"};
//    private int[] imageId = {R.mipmap.appointment,
//            R.mipmap.appointment_record,
//            R.mipmap.icon10,
//            R.mipmap.icon12,
//            R.mipmap.icon14,
//            R.mipmap.icon13,
//            R.mipmap.icon6, R.mipmap.dingqi, R.mipmap.dingqi
//    };
//    private int[] colors = {R.drawable.radius_a1, R.drawable.radius_a2,
//            R.drawable.radius_a3, R.drawable.radius_a4,
//            R.drawable.radius_d3, R.drawable.radius_c5, R.drawable.radius_e3, R.drawable.radius_a1,
//            R.drawable.radius_a2};

    private String[] str = {
            "考试预约", "预约查询", "预约一览"
    };
    private int[] imageId = {
            R.mipmap.appointment,
            R.mipmap.appointment_record,
            R.mipmap.icon13
    };
    private int[] colors = {
            R.drawable.radius_a1,
            R.drawable.radius_a2,
            R.drawable.radius_a3
    };
    private List<Integer> item;

    private String idCardNo;
    private String xm;


    //    android:background="@drawable/home_bg"


    @Override
    protected ActivityMainBinding getBinding() {
        return ActivityMainBinding.inflate(LayoutInflater.from(this));
    }

    @Override
    protected Toolbar getToolbar() {
        return binding.toolbarView.toolbar;
    }

    @Override
    protected void initView() {

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        binding.toolbarView.title.setText("承包商考试预约");
        mToolbar.setBackgroundColor(Color.parseColor("#00000000"));
        gv = findViewById(R.id.gv);
        setExitZx();
    }

    @Override
    public void initData() {

        Intent intent = getIntent();
        idCardNo = intent.getStringExtra("idCardNo");
        xm = intent.getStringExtra("xm");

        dialogDatas = new ArrayList<>();
        dialogDatas2 = new ArrayList<>();


        item = new ArrayList<>();

        beanList.addAll(getGridViewData(isTask));

        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new GridViewAdapter(this, beanList, item, true);
        gv.setAdapter(adapter);


        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent;
                switch (position) {
                    case 0:

//                        intent = new Intent();
//                        intent.setClass(MainActivity.this, ExamAppointmentActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                        intent.putExtra("idCardNo", idCardNo);
//                        startActivity(intent);

                        break;
                    case 1:

//                        intent = new Intent();
//                        intent.setClass(MainActivity.this, AppointmentListActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                        intent.putExtra("idCardNo", idCardNo);
//                        startActivity(intent);

                        break;
                    case 2:

//                        intent = new Intent();
//                        intent.setClass(MainActivity.this, SimpleActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                        intent.putExtra("idCardNo", idCardNo);
//                        startActivity(intent);

                        break;
                    default:
                        break;
                }
            }
        });
    }

    // 按两次返回键退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                ControllerActivity.getAppManager().finishAllActivity();
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setExitZx() {
        mToolbar.setNavigationIcon(R.mipmap.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("你确定要退出程序?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ControllerActivity.getAppManager().finishAllActivity();
                        finish();
                        System.exit(0);
                        dialog.dismiss();
                    }
                });
                builder.create().show();

            }
        });
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                if(item.getItemId()==R.id.zx){
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setMessage("您确定要注销吗?");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ControllerActivity.getAppManager().finishAllActivity();
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivity(intent);
                            //finish();
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
                return true;
            }
        });
    }

    /**
     * @param isTask 是否有行为安全观察任务
     * @return
     */
    private List<GridViewBean> getGridViewData(boolean isTask) {

        List<GridViewBean> datas = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            GridViewBean bean = new GridViewBean();
            bean.setTitle(str[i]);
            bean.setBackgroup(colors[i]);
            bean.setImageid(imageId[i]);
            bean.setShow(isTask);
            datas.add(bean);
        }

        return datas;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}