package com.casic.dcms.fragment;

import android.content.Intent;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.databinding.FragmentHomeBinding;
import com.casic.dcms.model.HomePageBean;
import com.casic.dcms.model.UserBean;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.view.BridgeMaintainActivity;
import com.casic.dcms.view.OvertimeCaseActivity;
import com.casic.dcms.view.PersonActivity;
import com.casic.dcms.view.PipelineManagerActivity;
import com.casic.dcms.view.SearchCaseActivity;
import com.casic.dcms.view.StreetlightManagerActivity;
import com.casic.dcms.view.business.ChangeShopInfoActivity;
import com.casic.dcms.view.business.PackageCaseActivity;
import com.casic.dcms.view.check.CheckCaseActivity;
import com.casic.dcms.view.department.DeptHandleActivity;
import com.casic.dcms.view.handle.HandleCaseActivity;
import com.casic.dcms.view.map.CaseOnMapActivity;
import com.casic.dcms.view.statistics.DataAnalysisActivity;
import com.casic.dcms.view.toilet.PublicToiletActivity;
import com.casic.dcms.view.upload.BriefCaseActivity;
import com.casic.dcms.view.upload.BusinessCaseActivity;
import com.casic.dcms.view.upload.CommonCaseActivity;
import com.casic.dcms.view.urgent.UrgentCaseActivity;
import com.casic.dcms.view.verify.VerifyCaseActivity;
import com.casic.dcms.vm.HomePageViewModel;
import com.pengxh.androidx.lite.adapter.NormalRecyclerAdapter;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.hub.LongHub;
import com.pengxh.androidx.lite.utils.SaveKeyValues;

import java.util.GregorianCalendar;

public class HomePageFragment extends AndroidxBaseFragment<FragmentHomeBinding> {

    private HomePageViewModel homePageViewModel;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initData() {
        //首页相关功能项
        homePageViewModel = new ViewModelProvider(this).get(HomePageViewModel.class);
        homePageViewModel.obtainHomePageData("05");
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    public void onResume() {
        String lastTime = (String) SaveKeyValues.getValue(LocaleConstant.LAST_LOGIN, "");
        String timeValue;
        if ("".equals(lastTime)) {
            timeValue = "上次登录时间：无法确定";
        } else {
            timeValue = "上次登录时间：" + lastTime;
        }
        viewBinding.lastTimeView.setText(timeValue);
        //用户信息
        UserBean.DataBean userBean = StringHelper.getUserBean();
        //获取用户信息之后保存登录时间
        SaveKeyValues.putValue(LocaleConstant.LAST_LOGIN, LongHub.timestampToCompleteDate(System.currentTimeMillis()));

        GregorianCalendar calendar = new GregorianCalendar();
        int x = calendar.get(GregorianCalendar.AM_PM);
        String timezone;
        if (x == 0) {
            timezone = "上午";
        } else {
            timezone = "下午";
        }
        Glide.with(this).load(R.mipmap.app_logo).into(viewBinding.userIconView);
        viewBinding.userNameView.setText(timezone + "好！" + userBean.getName());
        viewBinding.userDeptView.setText(userBean.getDeptName() + userBean.getRoleNames());
        super.onResume();
    }

    @Override
    protected void initEvent() {
        homePageViewModel.homePageDataModel.observe(this, new Observer<HomePageBean>() {
            @Override
            public void onChanged(HomePageBean resultBean) {
                if (resultBean.getCode() == 200) {
                    NormalRecyclerAdapter<HomePageBean.DataBean.MenusBean> recyclerAdapter = new NormalRecyclerAdapter<HomePageBean.DataBean.MenusBean>(R.layout.item_home_rv_g, resultBean.getData().getMenus()) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, HomePageBean.DataBean.MenusBean item) {
                            viewHolder.setImageResource(R.id.imageView, StringHelper.obtainIcon(item.getName())).setText(R.id.textView, item.getName());
                        }
                    };
                    viewBinding.homeRecyclerView.setAdapter(recyclerAdapter);
                    recyclerAdapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<HomePageBean.DataBean.MenusBean>() {
                        @Override
                        public void onItemClicked(int position, HomePageBean.DataBean.MenusBean menusBean) {
                            Intent intent = new Intent();
                            switch (menusBean.getName()) {
                                case "案卷上报":
                                    intent.setClass(requireContext(), CommonCaseActivity.class);
                                    break;
                                case "快速上报":
                                    intent.setClass(requireContext(), BriefCaseActivity.class);
                                    break;
                                case "三包上报":
                                    intent.setClass(requireContext(), BusinessCaseActivity.class);
                                    break;
                                case "案卷核实":
                                    intent.setClass(requireContext(), VerifyCaseActivity.class);
                                    break;
                                case "案卷核查":
                                    intent.setClass(requireContext(), CheckCaseActivity.class);
                                    break;
                                case "案卷处理":
                                    intent.setClass(requireContext(), HandleCaseActivity.class);
                                    break;
                                case "紧急要件":
                                    intent.setClass(requireContext(), UrgentCaseActivity.class);
                                    break;
                                case "超时督办":
                                    intent.setClass(requireContext(), OvertimeCaseActivity.class);
                                    break;
                                case "统计分析":
                                    intent.setClass(requireContext(), DataAnalysisActivity.class);
                                    break;
                                case "在岗人员":
                                    intent.setClass(requireContext(), PersonActivity.class);
                                    break;
                                case "案卷查询":
                                    intent.setClass(requireContext(), SearchCaseActivity.class);
                                    break;
                                case "案卷分布":
                                    intent.setClass(requireContext(), CaseOnMapActivity.class);
                                    break;
                                case "公厕保洁":
                                    intent.setClass(requireContext(), PublicToiletActivity.class);
                                    break;
                                case "三包处置":
                                    intent.setClass(requireContext(), PackageCaseActivity.class);
                                    break;
                                case "商铺变更":
                                    intent.setClass(requireContext(), ChangeShopInfoActivity.class);
                                    break;
                                case "部门处置":
                                    intent.setClass(requireContext(), DeptHandleActivity.class);
                                    break;
                                case "路桥养护":
                                    intent.setClass(requireContext(), BridgeMaintainActivity.class);
                                    break;
                                case "路灯管理":
                                    intent.setClass(requireContext(), StreetlightManagerActivity.class);
                                    break;
                                case "管网管理":
                                    intent.setClass(requireContext(), PipelineManagerActivity.class);
                                    break;
                            }
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }
}