package com.cooleshow.student.ui.main;

import android.Manifest;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.PermissionUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.widgets.DialogUtil;
import com.cooleshow.student.R;
import com.cooleshow.student.adapter.ItemMarkAdapter;
import com.cooleshow.student.bean.StudentUserInfo;
import com.cooleshow.student.contract.MineContract;
import com.cooleshow.student.databinding.FragmentMineLayoutBinding;
import com.cooleshow.student.presenter.main.MinePresenter;
import com.tbruyelle.rxpermissions3.RxPermissions;

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

/**
 * 创建日期：2022/5/26 10:03
 *
 * @author Ryan
 * 类说明：
 */
public class MineFragment extends BaseMVPFragment<FragmentMineLayoutBinding, MinePresenter> implements MineContract.MineView, View.OnClickListener {
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_avatar:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //个人中心
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_PERSONAL_SETTING)
                        .navigation();
                break;
            case R.id.iv_setting:
                //设置
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_SETTING)
                        .navigation();
                break;
            case R.id.im_vip:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //会员中心
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.STUDENT_MEMBER_CENTER)
                        .navigation();
                break;
            case R.id.ll_residue_class:
            case R.id.ll_finished_class:
            case R.id.ll_mine_course:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //我的课程
                ARouter.getInstance().build(RouterPath.CourseCenter.TEACHER_MINE_COURSE)
                        .navigation();
                break;
            case R.id.ll_mine_homework:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //我的作业
                ARouter.getInstance().build(RouterPath.WorkCenter.TEACHER_WORK_HOMEWORK)
                        .navigation();
                break;
            case R.id.ll_mine_score:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //我的乐谱
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.STUDENT_MUSIC_PERSONAL)
                        .navigation();
                break;
            case R.id.ll_meter_records:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //评测记录
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.STUDENT_MEMBER_RECORD)
                        .navigation();
                break;
            case R.id.tv_goods_order:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //商品订单
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.MALL_GOODS_ORDER)
                        .navigation();
                break;
            case R.id.tv_transaction_record:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //交易记录
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.STUDENT_TRAD_RECORD)
                        .navigation();
                break;
            case R.id.tv_network_detection:
                //网络检测
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_NETWORK_MONITORING)
                        .navigation();
                break;
            case R.id.tv_device_detection:
                //设备检测
                checkPermission();
                //支付测试
               /* ARouter.getInstance().build(RouterPath.MineCenter.MINE_PAY_TEST)
                        .navigation();*/
                break;
            case R.id.tv_feedback:
                //意见反馈
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_FEEDBACK)
                        .navigation();
                break;
            case R.id.tv_help:
                //帮助中心
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.HELP_CENTER)
                        .navigation();
                break;
            case R.id.ll_follow_teacher:
                //关注老师
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.STUDENT_TEACHER_FOLLOW)
                        .navigation();
                break;
            case R.id.iv_scan:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //二维码扫描
                ARouter.getInstance()
                        .build(RouterPath.BaseCenter.SCAN_QR_CODE)
                        .navigation();
                break;
        }
    }

    private void checkPermission() {
        new RxPermissions(this)
                .request(Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                        ARouter.getInstance().build(RouterPath.MineCenter.MINE_EQUIPMENT_TEST)
                                .navigation();
                    } else {
                        DialogUtil.showInCenter(getChildFragmentManager(), com.cooleshow.base.R.layout.common_popu_stu, (holder, dialog) -> {
                            TextView tvTitle = holder.getView(com.cooleshow.base.R.id.tv_title);
                            TextView tvContent = holder.getView(com.cooleshow.base.R.id.tv_content);
                            TextView btncancel = holder.getView(com.cooleshow.base.R.id.btn_cancel);
                            TextView btnCommit = holder.getView(com.cooleshow.base.R.id.btn_commit);
                            tvTitle.setText("提示");
                            tvContent.setText(getResources().getString(com.cooleshow.base.R.string.equipment_testing_hint));
                            btncancel.setOnClickListener(view1 -> {
                                dialog.dismiss();
                            });
                            btnCommit.setOnClickListener(view1 -> {
                                PermissionUtils.toSelfSetting(getContext());
                                dialog.dismiss();
                            });
                        });
                    }
                });
    }

    @Override
    protected FragmentMineLayoutBinding getLayoutView() {
        return FragmentMineLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected MinePresenter createPresenter() {
        return new MinePresenter();
    }

    private RecyclerView rv_mark;

    @Override
    protected void initView(View rootView) {
        mViewBinding.imVip.setOnClickListener(this);
        rv_mark = mViewBinding.rvMark;
        mViewBinding.ivAvatar.setOnClickListener(this);
        mViewBinding.ivSetting.setOnClickListener(this);
        mViewBinding.llMineScore.setOnClickListener(this);
        mViewBinding.llMineHomework.setOnClickListener(this);
        mViewBinding.llMineCourse.setOnClickListener(this);
        mViewBinding.llMeterRecords.setOnClickListener(this);
        mViewBinding.tvGoodsOrder.setOnClickListener(this);
        mViewBinding.tvTransactionRecord.setOnClickListener(this);
        mViewBinding.tvNetworkDetection.setOnClickListener(this);
        mViewBinding.tvDeviceDetection.setOnClickListener(this);
        mViewBinding.tvHelp.setOnClickListener(this);
        mViewBinding.llFollowTeacher.setOnClickListener(this);
        mViewBinding.llFinishedClass.setOnClickListener(this);
        mViewBinding.llResidueClass.setOnClickListener(this);
        mViewBinding.ivScan.setOnClickListener(this);
        mViewBinding.tvFeedback.setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.queryUserInfo();
    }

    @Override
    public void queryUserInfoSuccess(StudentUserInfo data) {
        if (null == data) {
            return;
        }
        GlideUtils.INSTANCE.loadImage(getContext(), data.heardUrl, mViewBinding.ivAvatar, R.drawable.icon_student_default_head);
        mViewBinding.tvNickname.setText(data.username);
        mViewBinding.tvCardId.setText("学号：" + data.userId);
        mViewBinding.tvValidity.setText(data.membershipDays + "");
        mViewBinding.tvFinishedClass.setText(data.finshClassHours + "");
        mViewBinding.tvResidueClass.setText(data.unfinshClassHours + "");
        mViewBinding.tvFocusTeacher.setText(data.starTeacherNum + "");
        if (!TextUtils.isEmpty(data.subjectName)) {
            rv_mark.setVisibility(View.VISIBLE);
            List<String> markList = new ArrayList<>();
            if (data.subjectName.contains(",")) {
                String[] split = data.subjectName.split(",");
                for (String s : split) {
                    markList.add(s);
                }
            } else {
                markList.add(data.subjectName);
            }
            LinearLayoutManager manager = new LinearLayoutManager(getContext());
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_mark.setLayoutManager(manager);
            ItemMarkAdapter itemMarkAdapter = new ItemMarkAdapter(markList);
            rv_mark.setAdapter(itemMarkAdapter);
        } else {
            rv_mark.setVisibility(View.GONE);
        }

    }
}
