package com.casic.dcms.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.adapter.HomeRecycleAdapter;
import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.HomePageBean;
import com.casic.dcms.bean.UserBean;
import com.casic.dcms.databinding.FragmentHomeBinding;
import com.casic.dcms.mvp.presenter.impl.HomePagePresenterImpl;
import com.casic.dcms.mvp.presenter.impl.UserDataPresenterImpl;
import com.casic.dcms.mvp.view.IHomePageView;
import com.casic.dcms.mvp.view.IUserDataView;
import com.casic.dcms.ui.BridgeMaintainActivity;
import com.casic.dcms.ui.CaseCheckActivity;
import com.casic.dcms.ui.CaseHandleActivity;
import com.casic.dcms.ui.CaseOnMapActivity;
import com.casic.dcms.ui.CaseSearchActivity;
import com.casic.dcms.ui.CaseUploadActivity;
import com.casic.dcms.ui.CaseUploadPackageActivity;
import com.casic.dcms.ui.CaseUploadQuicklyActivity;
import com.casic.dcms.ui.CaseVerifyActivity;
import com.casic.dcms.ui.ChangeShopInfoActivity;
import com.casic.dcms.ui.DataAnalysisActivity;
import com.casic.dcms.ui.DeptHandleActivity;
import com.casic.dcms.ui.OvertimeCaseActivity;
import com.casic.dcms.ui.PackageHandleActivity;
import com.casic.dcms.ui.PersonOnlineActivity;
import com.casic.dcms.ui.PipelineManagerActivity;
import com.casic.dcms.ui.PublicToiletActivity;
import com.casic.dcms.ui.StreetlightManagerActivity;
import com.casic.dcms.ui.UrgentCaseActivity;
import com.casic.dcms.utils.SaveKeyValues;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.casic.dcms.utils.ToastHelper;
import com.google.gson.Gson;

import java.util.GregorianCalendar;

/**
 * 此页面后期可能需要添加滑动吸顶效果
 */
public class HomePageFragment extends AndroidxBaseFragment<FragmentHomeBinding> implements IUserDataView, IHomePageView {

    private Context context;
    private UserDataPresenterImpl userDataPresenter;
    private HomePagePresenterImpl homePagePresenter;

    @Override
    protected void setupTopBarLayout() {
        context = getContext();
    }

    @Override
    protected void initData() {
        String lastTime = (String) SaveKeyValues.getValue(StringHelper.getUserAccount(), "");
        String timeValue;
        if ("".equals(lastTime)) {
            timeValue = "上次登录时间：无法确定";
        } else {
            timeValue = "上次登录时间：" + lastTime;
        }
        viewBinding.lastTimeView.setText(timeValue);

        homePagePresenter = new HomePagePresenterImpl(this);
        userDataPresenter = new UserDataPresenterImpl(this);

        //用户信息
        userDataPresenter.onReadyRetrofitRequest();
        //首页相关功能项
        homePagePresenter.onReadyRetrofitRequest("05");
    }

    @Override
    protected void initEvent() {
        Glide.with(this).load(R.mipmap.app_logo).into(viewBinding.userIconView);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void obtainUserData(UserBean userBean) {
        if (userBean.getCode() == 200) {
            UserBean.DataBean dataBean = userBean.getData();
            StringHelper.saveUserBean(new Gson().toJson(dataBean));
            //获取用户信息之后保存登录时间
            SaveKeyValues.putValue(dataBean.getAccount(), TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()));

            GregorianCalendar calendar = new GregorianCalendar();
            int x = calendar.get(GregorianCalendar.AM_PM);
            String timezone;
            if (x == 0) {
                timezone = "上午";
            } else {
                timezone = "下午";
            }
            viewBinding.userNameView.setText(timezone + "好！" + dataBean.getName());
            viewBinding.userDeptView.setText(dataBean.getDeptName() + dataBean.getRoleNames());
        }
    }

    public void obtainDataFail() {
        ToastHelper.showToast("获取用户信息失败，请重试", ToastHelper.ERROR);
    }

    @Override
    public void obtainHomePageResult(HomePageBean resultBean) {
        if (resultBean.getCode() == 200) {
            HomeRecycleAdapter adapter = new HomeRecycleAdapter(context, resultBean.getData().getMenus());
            viewBinding.homeRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
            viewBinding.homeRecyclerView.setAdapter(adapter);
            adapter.setOnGridItemClickListener(new HomeRecycleAdapter.OnGridItemClickListener() {
                @Override
                public void onClick(String itemName) {
                    Intent intent = new Intent();
                    switch (itemName) {
                        case "案卷上报":
                            intent.setClass(context, CaseUploadActivity.class);
                            break;
                        case "快速上报":
                            intent.setClass(context, CaseUploadQuicklyActivity.class);
                            break;
                        case "三包上报":
                            intent.setClass(context, CaseUploadPackageActivity.class);
                            break;
                        case "案卷核实":
                            intent.setClass(context, CaseVerifyActivity.class);
                            break;
                        case "案卷核查":
                            intent.setClass(context, CaseCheckActivity.class);
                            break;
                        case "案卷处理":
                            intent.setClass(context, CaseHandleActivity.class);
                            break;
                        case "紧急要件":
                            intent.setClass(context, UrgentCaseActivity.class);
                            break;
                        case "超时督办":
                            intent.setClass(context, OvertimeCaseActivity.class);
                            break;
                        case "统计分析":
                            intent.setClass(context, DataAnalysisActivity.class);
                            break;
                        case "在岗人员":
                            intent.setClass(context, PersonOnlineActivity.class);
                            break;
                        case "案卷查询":
                            intent.setClass(context, CaseSearchActivity.class);
                            break;
                        case "案卷分布":
                            intent.setClass(context, CaseOnMapActivity.class);
                            break;
                        case "公厕保洁":
                            intent.setClass(context, PublicToiletActivity.class);
                            break;
                        case "三包处置":
                            intent.setClass(context, PackageHandleActivity.class);
                            break;
                        case "商铺变更":
                            intent.setClass(context, ChangeShopInfoActivity.class);
                            break;
                        case "部门处置":
                            intent.setClass(context, DeptHandleActivity.class);
                            break;
                        case "路桥养护":
                            intent.setClass(context, BridgeMaintainActivity.class);
                            break;
                        case "路灯管理":
                            intent.setClass(context, StreetlightManagerActivity.class);
                            break;
                        case "管网管理":
                            intent.setClass(context, PipelineManagerActivity.class);
                            break;
                    }
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        userDataPresenter.disposeRetrofitRequest();
        homePagePresenter.disposeRetrofitRequest();
        super.onDestroyView();
    }
}