package com.cooleshow.teacher.ui.main

import android.util.Log
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.cooleshow.base.common.WebConstants
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.loadImage
import com.cooleshow.base.utils.Utils
import com.cooleshow.teacher.R
import com.cooleshow.teacher.bean.CountOfUnreadBean
import com.cooleshow.teacher.bean.HelpCenterContentBean
import com.cooleshow.teacher.bean.HomeCountBean
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.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.cardMineAssess.setOnClickListener(this)
        mViewBinding.cardAfterClassAssess.setOnClickListener(this)
        mViewBinding.ivCertificationStatus.setOnClickListener(this)
        mViewBinding.cardMineMusicSheet.setOnClickListener(this)
        mViewBinding.cardAfterClassHomework.setOnClickListener(this)
        mViewBinding.cardMineIncome.setOnClickListener(this)
        mViewBinding.ivChat.setOnClickListener(this)
        mViewBinding.tvNoticeContent.setOnClickListener(this)
        mViewBinding.imNoticeList.setOnClickListener(this)
        mViewBinding.cardKotofusa.setOnClickListener(this)
    }

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


    fun getTeacherInfoSuccess(teacherUserInfo: TeacherUserInfo) {
        Log.e("sdfafdafdad", "getTeacherInfoSuccess: ")
        if (isDetached) {
            return;
        }
        if (context == null || teacherUserInfo == null) {
            return
        }
        //头像
        loadImage(
            requireContext(),
            teacherUserInfo.heardUrl,
            mViewBinding.ivAvatar,
            R.drawable.icon_teacher_default_head
        )
        //昵称
        mViewBinding.tvNickname.text =
            UserHelper.getTeacherName(teacherUserInfo.username, teacherUserInfo.userId);
        //星级
        if (teacherUserInfo.starGrade.toFloat() > 0) {
            mViewBinding.ratingBar.visibility = View.VISIBLE
            mViewBinding.tvNoScore.visibility = View.GONE
            mViewBinding.ratingBar.rating = teacherUserInfo.starGrade.toFloat()
        } else {
            mViewBinding.ratingBar.visibility = View.GONE
            mViewBinding.tvNoScore.visibility = View.VISIBLE
        }


        //粉丝数
        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.visibility = View.VISIBLE
                mViewBinding.ivUnCertificationTip.visibility = View.GONE
                mViewBinding.ivTeacherEntryTag.visibility = View.GONE
                mViewBinding.groupTeacherExtra.visibility = View.GONE
            }
            TeacherInfoConstants.ENTRY_STATUS_PASS -> {
                //入驻审核通过
                mViewBinding.ivCertificationStatus.visibility = View.GONE
                mViewBinding.ivUnCertificationTip.visibility = View.GONE
                mViewBinding.groupTeacherExtra.visibility = View.VISIBLE
                mViewBinding.ivTeacherEntryTag.visibility = View.VISIBLE
            }
            else -> {
                //未通过或者未申请
                mViewBinding.ivCertificationStatus.setImageResource(R.drawable.icon_home_un_certification)
                mViewBinding.groupTeacherExtra.visibility = View.GONE
                mViewBinding.ivCertificationStatus.visibility = View.VISIBLE
                mViewBinding.ivUnCertificationTip.visibility = View.VISIBLE
                mViewBinding.ivTeacherEntryTag.visibility = View.GONE
            }
        }
        musicianAuthStatus = teacherUserInfo.musicianAuthStatus
        when (teacherUserInfo.musicianAuthStatus) {
            TeacherInfoConstants.MUSICIAN_STATUS_PASS -> {
                mViewBinding.ivTeacherMusicPersonTag.visibility = View.VISIBLE
            }
            else -> {
                mViewBinding.ivTeacherMusicPersonTag.visibility = View.INVISIBLE
            }
        }
    }

    private var musicianAuthStatus: String? = null

    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()
            }
            R.id.iv_certification_status -> {
                //老师认证
                ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.TEACHER_CERT)
                    .navigation()
            }
            R.id.card_mine_music_sheet -> {
                ARouter.getInstance().build(RouterPath.ScoreCenter.TEACHER_MINE_SCORE)
                    .withString("musicianAuthStatus", musicianAuthStatus)
                    .navigation()
            }
            R.id.card_after_class_homework -> {
                ARouter.getInstance().build(RouterPath.WorkCenter.TEACHER_WORK_HOMEWORK)
                    .navigation()
            }
            R.id.card_mine_income -> {
                ARouter.getInstance().build(RouterPath.IncomeCenter.TEACHER_MINE_INCOME)
                    .navigation()
            }
            R.id.iv_chat -> {
                ARouter.getInstance().build(RouterPath.MessageCenter.TEACHER_MESSAGE_MESSAGEBOX)
                    .navigation()
            }
            R.id.tv_notice_content -> {
                //点击单条公告
                ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.HELP_CENTER_DETAIL + noticeId)
                    .navigation()
            }
            R.id.im_notice_list -> {
                //公告列表
                ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.HELP_CENTER_CATALOGTYPE_2)
                    .navigation()
            }
            R.id.card_Kotofusa->{
                //云库琴房
                ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.PIANO_ROOM)
                    .navigation()
            }
        }

    }

    override fun onResume() {
        super.onResume()
        presenter.getHomeCount();
        presenter.queryCountOfUnread()
        presenter.helpCenterContentList()
    }

    override fun getHomeCountSuccess(data: HomeCountBean?) {
        if (data != null) {
            mViewBinding.tvMineCourseCount.text = "本周剩余\n" + data.courseSchedule + "课时"
            mViewBinding.tvAfterClassHomeworkCount.text = "本周共" + data.courseHomework + "节课\n未布置作业"
            mViewBinding.tvAfterClassAssessCount.text =
                "剩余" + data.courseScheduleReplied + "节\n尚未评价"
            mViewBinding.tvMineMusicSheetCount.text = "共" + data.musicSheet + "支\n乐谱"
            mViewBinding.tvMineAssessCount.text = "本周收到\n" + data.studentReplied + "个评价"
            mViewBinding.tvMineIncomeCount.text = "本月收入\n" + data.decimal + "元"
            mViewBinding.tvMineKotofusaCount.text="剩余\n"+data.pianoTime + "分钟"
        }
    }

    private var noticeId: Long = 0
    override fun helpCenterContentListSuccess(data: HelpCenterContentBean?) {

        if (data?.rows != null && data.rows.size > 0) {
            mViewBinding.tvNoticeContent.text = data.rows[0].title
            noticeId = data.rows[0].id
        }

    }

    override fun queryCountOfUnreadSuccess(data: MutableList<CountOfUnreadBean>?) {
        if (null == data || data.size == 0) {
            mViewBinding.viewUnreadMessage.setVisibility(View.GONE)
            return
        }
        var totalCount = 0
        for (datum in data) {
            totalCount += datum.value
        }
        if (totalCount > 0) {
            mViewBinding.viewUnreadMessage.setVisibility(View.VISIBLE)
        } else {
            mViewBinding.viewUnreadMessage.setVisibility(View.GONE)
        }
    }

}