package com.tenlionsoft.oamodule.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.beans.FuncBean;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.core.widget.base.BaseFragment;
import com.tenlionsoft.baselib.core.widget.base.FunctionNumAdapter;
import com.tenlionsoft.baselib.utils.LogUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者: adam
 * 日期: 2022/4/11 - 11:13 上午
 * 邮箱: itgaojian@163.com
 * 描述: OA首页
 */
@Route(path = PathConfig.PATH_MODULE_OA_FRAGMENT_HOME)
public class OaMainFragment extends BaseFragment {

    @BindView(R2.id.banner)
    Banner mBanner;
    @BindView(R2.id.tv_notify)
    TextView mTvNotify;
    @BindView(R2.id.rlv_funcs)
    RecyclerView mRlvFuncs;
    @BindView(R2.id.rlv_notice)
    RecyclerView mRlvNotice;
    @BindView(R2.id.ll_empty)
    LinearLayout mLlEmpty;
    @BindView(R2.id.srl_content)
    SmartRefreshLayout mSrlContent;
    private List<String> mTitles = Arrays.asList(
        "文档中心",
        "邮件管理",
        "通知公告",
        "日程管理",
        "用车管理",
        "会议管理",
        "日常工作",
        "工作报告",
        "督办事项");


    private int[] mRes = new int[]{
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon,
        R.drawable.ic_census_main_icon};

    private List<FuncBean> mFuncBeans;
    private FunctionNumAdapter mAdapter;
    private ActivityResultLauncher<Intent> mLauncher;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_oa_main;
    }

    @Override
    protected void setDataToView(View dataView) {
        ButterKnife.bind(this, dataView);
        LogUtils.e("OAMainFragment");

        setStateView(STATE_SUCCESS);
        mSrlView.setEnableRefresh(false);
        mSrlView.setEnableLoadMore(false);
        initContentView();

    }

    /**
     * 初始化页面
     */
    private void initContentView() {
        mFuncBeans = new ArrayList<>();
        for (int i = 0; i < mTitles.size(); i++) {
            FuncBean bean = new FuncBean();
            bean.setText(mTitles.get(i));
            bean.setIconRes(mRes[i]);
            bean.setHintNum(0);
            mFuncBeans.add(bean);
        }
        mAdapter = new FunctionNumAdapter(mActivity, mFuncBeans);
        mRlvFuncs.setLayoutManager(new GridLayoutManager(mActivity, 4));
        mRlvFuncs.setAdapter(mAdapter);
        mAdapter.addOnItemClickListener(this::choosePage);
    }

    /**
     * 功能跳转
     */
    private void choosePage(FuncBean funcBean) {
        String path = "";
        switch (funcBean.getText()) {
            case "文档中心":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_DOCUMENT;
                break;
            case "邮件管理":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_EMAIL;
                break;
            case "通知公告":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_NOTICE;
                break;
            case "日程管理":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_SCHEDULE;
                break;
            case "用车管理":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_CAR;
                break;
            case "会议管理":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_MEETING;
                break;
            case "日常工作":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_ROUTINE_WORK;
                break;
            case "工作报告":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_WORK_REPORT;
                break;
            case "督办事项":
                path = PathConfig.PATH_MODULE_OA_ACTIVITY_SUPERVISE;
                break;
        }
        ARouter.getInstance()
            .build(path)
            .navigation();
//   TODO     if ("文档中心".equals(funcBean.getText()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//            if (Environment.isExternalStorageManager()) {
//                ARouter.getInstance()
//                    .build(path)
//                    .navigation();
//            } else {
//                ToastUtils.show("该功能需要SD卡管理权限,请授权");
//                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
//                intent.setData(Uri.parse("package:" + mActivity.getPackageName()));
//                mLauncher.launch(intent);
//            }
//        } else {
//
//        }

    }


    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) getActivity();
        mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    if (!Environment.isExternalStorageManager()) {
                        //没有授权
                        ToastUtils.show("该功能需要SD卡的管理权限,请前往设置进行授权.");
                    } else {
                        ARouter.getInstance()
                            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_DOCUMENT)
                            .navigation();
                    }
                }
            });
    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void loadMoreData() {

    }
}
