package com.cooleshow.teacher.ui.main;

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

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.utils.Utils;
import com.cooleshow.base.utils.helper.WebStartHelper;
import com.cooleshow.base.widgets.DialogUtil;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.TeacherPromoteIncomeBean;
import com.cooleshow.teacher.bean.TeacherUserInfo;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.MineContract;
import com.cooleshow.teacher.databinding.FragmentMineLayoutBinding;
import com.cooleshow.teacher.presenter.main.MinePresenter;
import com.cooleshow.usercenter.helper.UserHelper;
import com.tbruyelle.rxpermissions3.RxPermissions;

/**
 * Author by pq, Date on 2022/5/9.
 */
public class MineFragment extends BaseMVPFragment<FragmentMineLayoutBinding, MinePresenter> implements MineContract.MineView, View.OnClickListener {
    private String teacherCertStatus = TeacherInfoConstants.ENTRY_STATUS_UNPAALY;

    @Override
    protected void initView(View rootView) {
        Utils.setHeadView(mViewBinding.viewStatusBar, requireContext(), 0);
        mViewBinding.ivCertificationStatus.setOnClickListener(this);
        mViewBinding.ivUnCertificationTip.setOnClickListener(this);
        mViewBinding.tvMinePage.setOnClickListener(this);
        mViewBinding.tvMineStyle.setOnClickListener(this);
        mViewBinding.ivAvatar.setOnClickListener(this);
        mViewBinding.ivSetting.setOnClickListener(this);
        mViewBinding.tvSparingCourseSet.setOnClickListener(this);
        mViewBinding.tvLiveCourse.setOnClickListener(this);
        mViewBinding.tvVideoCourse.setOnClickListener(this);
        mViewBinding.tvHelp.setOnClickListener(this);
        mViewBinding.tvFeedback.setOnClickListener(this);
        mViewBinding.tvAuthentication.setOnClickListener(this);
        mViewBinding.tvOpenLive.setOnClickListener(this);
        mViewBinding.llFans.setOnClickListener(this);
        mViewBinding.tvMyAccount.setOnClickListener(this);
        mViewBinding.tvNetworkMonitoring.setOnClickListener(this);
        mViewBinding.tvEquipmentTesting.setOnClickListener(this);
        mViewBinding.llCourseNumCompleted.setOnClickListener(this);
        mViewBinding.llCourseNumNotStart.setOnClickListener(this);
        mViewBinding.ivGoodsOrder.setOnClickListener(this);
        mViewBinding.viewVipBg.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    //是否有直播权限 0 否 1 是
    private int liveFlag;

    public void getTeacherInfoSuccess(TeacherUserInfo teacherUserInfo) {
        if (isDetached() || !isAdded()) {
            return;
        }
        liveFlag = teacherUserInfo.liveFlag;
        //头像
        GlideUtils.INSTANCE.loadImage(requireContext(), teacherUserInfo.heardUrl, mViewBinding.ivAvatar, R.drawable.icon_teacher_default_head);
        //昵称
        mViewBinding.tvNickname.setText(UserHelper.getTeacherName(teacherUserInfo.username, teacherUserInfo.userId));
        //星级
        if (teacherUserInfo.starGrade > 0) {
            mViewBinding.ratingBar.setVisibility(View.VISIBLE);
            mViewBinding.ratingBar.setRating(teacherUserInfo.starGrade);
            mViewBinding.tvNoScore.setVisibility(View.GONE);
        } else {
            mViewBinding.ratingBar.setVisibility(View.GONE);
            mViewBinding.tvNoScore.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(teacherUserInfo.tag)) {
            mViewBinding.llBadge.setVisibility(View.VISIBLE);
            mViewBinding.llBadge.setVipIcon(teacherUserInfo.isVip == 1);
            mViewBinding.llBadge.setData(teacherUserInfo.tag);
        } else {
            if (teacherUserInfo.isVip == 1) {
                mViewBinding.llBadge.setVipIcon(true);
                mViewBinding.llBadge.setVisibility(View.VISIBLE);
            } else {
                mViewBinding.llBadge.setVisibility(View.GONE);
            }
        }

        //粉丝数
        mViewBinding.tvFansNum.setText(teacherUserInfo.fansNum);
        //已上课时数
        mViewBinding.tvCourseNumCompleted.setText(teacherUserInfo.expTime);
        //未上课时数
        mViewBinding.tvCourseNumNotStart.setText(teacherUserInfo.unExpTime);
        //会员天数
        mViewBinding.tvValidity.setText(teacherUserInfo.membershipDays);
        //达人认证状态
        this.teacherCertStatus = teacherUserInfo.entryStatus;
        if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_DOING)) {
            //入驻审核中
            mViewBinding.ivCertificationStatus.setImageResource(R.drawable.icon_home_entry_doing);
            mViewBinding.ivCertificationStatus.setVisibility(View.VISIBLE);
            hintCertificationOtherView();
        } else if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
            //入驻审核通过
            mViewBinding.ivCertificationStatus.setVisibility(View.GONE);
            showCertificationOtherView();
        } else {
            //未通过或者未申请
            mViewBinding.ivCertificationStatus.setImageResource(R.drawable.icon_home_un_certification);
            mViewBinding.ivCertificationStatus.setVisibility(View.VISIBLE);
            hintCertificationOtherView();
        }

        //银行卡绑定状态
        if (teacherUserInfo.isBank == 0) {
            mViewBinding.tvBankCardBindTip.setText("未绑定");
        } else {
            mViewBinding.tvBankCardBindTip.setText("已绑定");
        }
    }

    private void hintCertificationOtherView() {
        mViewBinding.llFans.setVisibility(View.GONE);
        mViewBinding.viewLine1.setVisibility(View.GONE);
        mViewBinding.llCourseNumCompleted.setVisibility(View.GONE);
        mViewBinding.viewLine2.setVisibility(View.GONE);
        mViewBinding.llCourseNumNotStart.setVisibility(View.GONE);
    }

    private void showCertificationOtherView() {
        mViewBinding.llFans.setVisibility(View.VISIBLE);
        mViewBinding.viewLine1.setVisibility(View.VISIBLE);
        mViewBinding.llCourseNumCompleted.setVisibility(View.VISIBLE);
        mViewBinding.viewLine2.setVisibility(View.VISIBLE);
        mViewBinding.llCourseNumNotStart.setVisibility(View.VISIBLE);
    }

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

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_certification_status:
                //达人认证
                goCert();
                break;
            case R.id.iv_un_certification_tip:
                //关闭认证提示
                mViewBinding.ivUnCertificationTip.setVisibility(View.GONE);
                break;
            case R.id.tv_mine_page:
                if (UiUtils.isFastClick()) {
                    return;
                }
                if (TextUtils.equals(teacherCertStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
                    //个人主页
                    ARouter.getInstance().build(RouterPath.MineCenter.HOME_PAGE)
                            .navigation();
                } else {
                    goCert();
                }
                break;
            case R.id.tv_mine_style:
                if (UiUtils.isFastClick()) {
                    return;
                }
                if (TextUtils.equals(teacherCertStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
                    //个人风采
                    ARouter.getInstance().build(RouterPath.MineCenter.MINE_STYLE_PAGE)
                            .navigation();
                } else {
                    goCert();
                }
                break;
            case R.id.iv_avatar:
                //个人设置
                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.tv_sparing_course_set:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //陪练课设置
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_PRACTICE_SETTING)
                        .navigation();
                break;
            case R.id.tv_live_course:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //直播课
                ARouter.getInstance().build(RouterPath.CourseCenter.TEACHER_MINE_LIVE_COURSE)
                        .navigation();
                break;
            case R.id.tv_video_course:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //视频课
                ARouter.getInstance().build(RouterPath.CourseCenter.TEACHER_MINE_VIDEO_COURSE)
                        .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.tv_feedback:
                //意见反馈
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_FEEDBACK)
                        .navigation();
                break;
            case R.id.tv_authentication:
                //银行卡
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_MY_BANKCARD)
                        .navigation();
                break;
            case R.id.tv_open_live:
                //开启直播
                if (UiUtils.isFastClick()) {
                    return;
                }
                if (liveFlag == 0) {
                    ARouter.getInstance()
                            .build(RouterPath.WebCenter.ACTIVITY_HTML)
                            .withString(WebConstants.WEB_URL, WebConstants.TEACHER_OPEN_LIVE)
                            .navigation();
                } else {
                    ARouter.getInstance().build(RouterPath.LiveCenter.TEACHER_LIVE_LIST)
                            .navigation();
                }

                break;
            case R.id.ll_fans:
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_MY_FANS)
                        .navigation();
                break;
            case R.id.tv_my_account:
                //音乐人认证
                WebStartHelper.startMusicPersonCertPage();
                break;
            case R.id.tv_network_monitoring:
                //网络检测
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_NETWORK_MONITORING)
                        .navigation();
                break;
            case R.id.tv_equipment_testing:
                //设备检测
                checkPermission();
                break;
            case R.id.ll_course_num_completed:
            case R.id.ll_course_num_not_start:
                ARouter.getInstance().build(RouterPath.CourseCenter.TEACHER_MINE_COURSE)
                        .navigation();
                break;
            //商品订单
            case R.id.iv_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.view_vip_bg:
                if (UiUtils.isFastClick()) {
                    return;
                }
                //会员中心
                ARouter.getInstance()
                        .build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_MEMBER_CENTER)
                        .navigation();
                break;

        }
    }

    private void goCert() {
        ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_HTML)
                .withString(WebConstants.WEB_URL, WebConstants.TEACHER_CERT)
                .navigation();
    }

    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();
                            });
                        });
                    }
                });
    }


    public void getTeacherPromoteInfoSuccess(TeacherPromoteIncomeBean incomeBean) {
        if (incomeBean != null && mViewBinding.tvPromoteIncomeTip != null) {
            mViewBinding.tvPromoteIncomeTip.setText(String.format("累计收益%s元", incomeBean.amountShare));
        }
    }
}
