package com.cooleshow.teacher.ui.main

import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.cooleshow.base.ext.setVisible
import com.cooleshow.base.router.RouterPath
import com.cooleshow.base.ui.fragment.BaseMVPFragment
import com.cooleshow.base.utils.GlideUtils
import com.cooleshow.base.utils.Utils
import com.cooleshow.teacher.R
import com.cooleshow.teacher.bean.TeacherUserInfo
import com.cooleshow.teacher.constants.TeacherInfoConstants
import com.cooleshow.teacher.contract.HomeContract
import com.cooleshow.teacher.databinding.FragmentHomeLayoutBinding
import com.cooleshow.teacher.presenter.main.HomePresenter
import com.cooleshow.usercenter.bean.UserInfo
import com.cooleshow.usercenter.helper.UserHelper


/**
 * Author by pq, Date on 2022/4/20.
 */
class HomeFragment : BaseMVPFragment<FragmentHomeLayoutBinding, HomePresenter>(),
    HomeContract.HomeView, View.OnClickListener {
    override fun getLayoutView(): FragmentHomeLayoutBinding {
        return FragmentHomeLayoutBinding.inflate(layoutInflater)
    }

    override fun initView(rootView: View?) {
        Utils.setHeadView(mViewBinding.viewStatusBar, requireContext(), 0)
    }

    override fun initData() {
        mViewBinding.cardMineCourse.setOnClickListener(this)
        mViewBinding.ivUnCertificationTip.setOnClickListener(this)
        mViewBinding.cardMineIncome.setOnClickListener(this)
        mViewBinding.cardMineAssess.setOnClickListener(this)
        mViewBinding.cardAfterClassAssess.setOnClickListener(this)
    }

    override fun createPresenter(): HomePresenter {
        return HomePresenter()
    }

    override fun onResume() {
        super.onResume()
        presenter.getUserInfo()
        presenter.getTeacherUserInfo()
    }

    override fun getUserInfoSuccess(userInfo: UserInfo?) {
        if (isDetached) {
            return;
        }
        if (context == null || userInfo == null) {
            return
        }
        //存储userInfo信息
        UserHelper.saveUserInfo(userInfo)
    }

    override fun getTeacherInfoSuccess(teacherUserInfo: TeacherUserInfo) {
        if (isDetached) {
            return;
        }
        if (context == null || teacherUserInfo == null) {
            return
        }
        //头像
        GlideUtils.loadImage(requireContext(), teacherUserInfo.heardUrl, mViewBinding.ivAvatar)
        //昵称
        mViewBinding.tvNickname.text =
            UserHelper.getTeacherName(teacherUserInfo.username, teacherUserInfo.userId);
        //星级
        mViewBinding.ratingBar.rating = teacherUserInfo.starGrade.toFloat()

        //粉丝数
        mViewBinding.tvFansNum.text = teacherUserInfo.fansNum;
        //已上课时数
        mViewBinding.tvCourseNumCompleted.text = teacherUserInfo.expTime
        //未上课时数
        mViewBinding.tvCourseNumNotStart.text = teacherUserInfo.unExpTime
        //入驻状态
        when (teacherUserInfo.entryStatus) {
            TeacherInfoConstants.ENTRY_STATUS_DOING -> {
                //入驻审核中
                mViewBinding.ivCertificationStatus.setImageResource(R.drawable.icon_home_entry_doing)
                mViewBinding.ivCertificationStatus.setVisible(true)
                mViewBinding.ivUnCertificationTip.setVisible(false)
                mViewBinding.ivTeacherEntryTag.setVisible(false)
                mViewBinding.groupTeacherExtra.setVisible(false)
            }
            TeacherInfoConstants.ENTRY_STATUS_PASS -> {
                //入驻审核通过
                mViewBinding.ivCertificationStatus.setVisible(false)
                mViewBinding.ivUnCertificationTip.setVisible(false)
                mViewBinding.groupTeacherExtra.setVisible(true)
                mViewBinding.ivTeacherEntryTag.setVisible(true)
            }
            else -> {
                //未通过或者未申请
                mViewBinding.ivCertificationStatus.setImageResource(R.drawable.icon_home_un_certification)
                mViewBinding.groupTeacherExtra.setVisible(false)
                mViewBinding.ivCertificationStatus.setVisible(true)
                mViewBinding.ivUnCertificationTip.setVisible(true)
                mViewBinding.ivTeacherEntryTag.setVisible(false)
            }
        }

        when (teacherUserInfo.musicianAuthStatus) {
            TeacherInfoConstants.MUSICIAN_STATUS_PASS -> {
                mViewBinding.ivTeacherMusicPersonTag.setVisible(true)
            }
            else -> {
                mViewBinding.ivTeacherMusicPersonTag.setVisible(false)
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_un_certification_tip -> {
                //关闭认证提示
                mViewBinding.ivUnCertificationTip.setVisible(false);
            }
            R.id.card_mine_course -> {
                //我的课程
                ARouter.getInstance().build(RouterPath.CourseCenter.TEACHER_MINE_COURSE)
                    .navigation()
            }
            R.id.card_mine_income -> {
                ARouter.getInstance().build(RouterPath.CourseCenter.SPARRING_COURSE_DETAIL)
                    .navigation()
            }
            R.id.card_mine_assess -> {
                ARouter.getInstance().build(RouterPath.CommentCenter.TEACHER_RECEIVED_COMMENT)
                    .navigation()
            }
            R.id.card_after_class_assess->{
                ARouter.getInstance().build(RouterPath.CommentCenter.TEACHER_COURSE_COMMENT)
                    .navigation()
            }
        }
    }

}