package com.casic.dcms.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
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.OvertimeCaseActivity;
import com.casic.dcms.view.SearchCaseActivity;
import com.casic.dcms.view.UrgentCaseActivity;
import com.casic.dcms.view.bridge.BridgeMaintainActivity;
import com.casic.dcms.view.check.CheckCaseActivity;
import com.casic.dcms.view.dept.DeptHandleActivity;
import com.casic.dcms.view.handle.HandleCaseActivity;
import com.casic.dcms.view.map.CaseOnMapActivity;
import com.casic.dcms.view.pipeline.PipelineManagerActivity;
import com.casic.dcms.view.sanbao.ChangeShopInfoActivity;
import com.casic.dcms.view.sanbao.SanBaoCaseActivity;
import com.casic.dcms.view.statistics.DataAnalysisActivity;
import com.casic.dcms.view.streetlight.LampBoxManagerActivity;
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.verify.VerifyCaseActivity;
import com.casic.dcms.view.worker.OnLineWorkerActivity;
import com.casic.dcms.vm.HomePageViewModel;
import com.casic.dcms.vm.UserViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.LongKit;
import com.pengxh.androidx.lite.utils.SaveKeyValues;

import java.util.GregorianCalendar;

@SuppressLint("SetTextI18n")
public class HomePageFragment extends AndroidxBaseFragment<FragmentHomeBinding> {

    private UserViewModel userViewModel;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
        userViewModel.userDataResult.observe(this, new Observer<UserBean>() {
            @Override
            public void onChanged(UserBean it) {
                if (it.getCode() == 200) {
                    GregorianCalendar calendar = new GregorianCalendar();
                    int x = calendar.get(GregorianCalendar.AM_PM);
                    String timezone;
                    if (x == 0) {
                        timezone = "上午";
                    } else {
                        timezone = "下午";
                    }
                    UserBean.DataBean bean = it.getData();

                    Glide.with(requireContext()).load(R.mipmap.app_logo).into(getBinding().userIconView);
                    getBinding().userNameView.setText(timezone + "好！" + bean.getName());
                    getBinding().userDeptView.setText(bean.getDeptName() + bean.getRoleNames());
                }
            }
        });

        //首页相关功能项
        HomePageViewModel homePageViewModel = new ViewModelProvider(this).get(HomePageViewModel.class);
        homePageViewModel.getHomePageData(requireContext(), "05");
        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());
                        }
                    };
                    getBinding().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(), OnLineWorkerActivity.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(), SanBaoCaseActivity.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(), LampBoxManagerActivity.class);
                                    break;
                                case "管网管理":
                                    intent.setClass(requireContext(), PipelineManagerActivity.class);
                                    break;
                            }
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void observeRequestState() {

    }

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

        userViewModel.getUserData(requireContext());
    }

    @Override
    protected void initEvent() {

    }
}