package com.stu.assistant.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.stu.assistant.R;
import com.stu.assistant.adapter.HomeGvAdapter;
import com.stu.assistant.base.BaseActivity;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.bean.MainModuleBean;
import com.stu.assistant.constants.ConstansItemManager;
import com.stu.assistant.presenter.MainPresenter;
import com.stu.assistant.uitls.MyToast;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;

import androidx.annotation.Nullable;
//主页面
public class MainActivity extends BaseActivity {

    private LinearLayout lay;
    private GridView gridView;
    private MainPresenter mainPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        setTitleLay(R.id.title_tv, R.id.back_lay, "学习助手");
        findViewById(R.id.back_lay).setVisibility(View.INVISIBLE);
        gridView = findViewById(R.id.main_gridview);
        lay = findViewById(R.id.lay);
    }


    @Override
    protected void initData() {
        mainPresenter = new MainPresenter();
    }

    @Override
    protected void initListener() {
        HomeGvAdapter homeGvAdapter = new HomeGvAdapter(this, mainPresenter.getData());
        gridView.setAdapter(homeGvAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                MainModuleBean moduleBean = ((MainModuleBean) parent.getAdapter().getItem(i));
                int itemFlag = moduleBean.getItemFlag();
                switch (itemFlag) {
                    case ConstansItemManager.item_1:
                        Intent intent1 = new Intent(mContext, TableListActivity.class);
                        intent1.putExtra("userLevel", SharedUtil.create(BaseApplion.application).getString(SharedConfig.userLevel));
                        intent1.putExtra("title", "课表");
                        intent1.putExtra("itemType", itemFlag);
                        startActivityForResult(intent1, 201);//查看课表
                        break;
                    case ConstansItemManager.item_2:
                        Intent intent = new Intent(mContext, TaskListActivity.class);
                        intent.putExtra("userLevel", SharedUtil.create(BaseApplion.application).getString(SharedConfig.userLevel));
                        intent.putExtra("title", "作业");
                        intent.putExtra("itemType", itemFlag);
                        startActivityForResult(intent, 202);//查看课表
                        break;
                    case ConstansItemManager.item_3:
                        Intent intent3 = new Intent(mContext, SettingActivity.class);
                        startActivity(intent3);//设置
                        finish();
                        break;
                    default:
                        break;
                }
            }
        });
    }


    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private static long lastClickTime;

    public void outApp() {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            MyToast.show("再按一次退出");
            lastClickTime = curClickTime;
        } else {
            BaseApplion.getActivityManage().finishAll();
        }
    }

    @Override
    public void onBackPressed() {
        outApp();
    }
}