package com.casic.dcms.ui.fragment;

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

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.casic.dcms.R;
import com.casic.dcms.adapter.HomeRecycleAdapter;
import com.casic.dcms.base.BaseFragment;
import com.casic.dcms.bean.HomePageBean;
import com.casic.dcms.bean.UserBean;
import com.casic.dcms.mvp.presenter.HomePagePresenterImpl;
import com.casic.dcms.mvp.presenter.UserDataPresenterImpl;
import com.casic.dcms.mvp.view.IHomePageView;
import com.casic.dcms.mvp.view.IUserDataView;
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.CaseUploadQuicklyActivity;
import com.casic.dcms.ui.CaseVerifyActivity;
import com.casic.dcms.ui.DataAnalysisActivity;
import com.casic.dcms.ui.OvertimeCaseActivity;
import com.casic.dcms.ui.PersonOnlineActivity;
import com.casic.dcms.ui.PublicToiletActivity;
import com.casic.dcms.ui.ThreePackageUploadActivity;
import com.casic.dcms.ui.UrgentCaseActivity;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;

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

    @BindView(R.id.userIconView)
    QMUIRadiusImageView userIconView;
    @BindView(R.id.userNameView)
    TextView userNameView;
    @BindView(R.id.userDeptView)
    TextView userDeptView;

    @BindView(R.id.homeRecyclerView)
    RecyclerView homeRecyclerView;

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

    @Override
    protected int initLayoutView() {
        return R.layout.fragment_home;
    }

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

    @Override
    protected void initData() {
        userDataPresenter = new UserDataPresenterImpl(this);
        userDataPresenter.onReadyRetrofitRequest();

        homePagePresenter = new HomePagePresenterImpl(this);
        homePagePresenter.onReadyRetrofitRequest("05");
    }

    @Override
    protected void initEvent() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void obtainUserData(UserBean userBean) {
        if (userBean.isSuccess()) {
            //QMUIRadiusImageView无法动态设置圆形头像，借助Glide实现圆形头像
            Glide.with(this).load(R.mipmap.app_logo).apply(RequestOptions.circleCropTransform()).into(userIconView);

            UserBean.DataBean data = userBean.getData();
            userNameView.setText(data.getName());
            userDeptView.setText(data.getDeptName() + data.getRoleNames());

            StringHelper.saveUserName(data.getName());
            StringHelper.saveUserPhone(data.getPhone());
            StringHelper.saveUserDept(data.getDeptName());
            StringHelper.saveUserDeptId(data.getDeptId());
            StringHelper.saveUserId(data.getId());
        }
    }


    @Override
    public void obtainHomePageResult(HomePageBean resultBean) {
        if (resultBean.isSuccess()) {
            HomeRecycleAdapter adapter = new HomeRecycleAdapter(context, resultBean.getData().getMenus());
            homeRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
            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, ThreePackageUploadActivity.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;
                        default:
                            break;
                    }
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void obtainDataFail() {
        ToastHelper.showToast("获取用户信息超时", ToastHelper.ERROR);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (userDataPresenter != null) {
            userDataPresenter.disposeRetrofitRequest();
        }
        if (homePagePresenter != null) {
            homePagePresenter.disposeRetrofitRequest();
        }
    }
}
