package com.cooleshow.teacher.ui.main;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.constanst.EventConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.TimeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.Utils;
import com.cooleshow.base.utils.helper.WebStartHelper;
import com.cooleshow.base.widgets.dialog.AwardTipDialog;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.HomeGoodMusicSheetAdapter;
import com.cooleshow.teacher.adapter.HomeHotAlbumAdapter;
import com.cooleshow.teacher.adapter.HomeHotMusicSheetAdapter;
import com.cooleshow.teacher.adapter.HomeMenuPageAdapter;
import com.cooleshow.teacher.bean.AwardStatusBean;
import com.cooleshow.teacher.bean.CountOfUnreadBean;
import com.cooleshow.teacher.bean.HelpCenterContentBean;
import com.cooleshow.teacher.bean.HomeCountBean;
import com.cooleshow.teacher.bean.HomeHotAlbumListBean;
import com.cooleshow.teacher.bean.HomeHotMusicSheetBean;
import com.cooleshow.teacher.bean.HomeHotMusicSheetItemBean;
import com.cooleshow.teacher.bean.HomeLiveAndVideoBean;
import com.cooleshow.teacher.bean.HomeMenuBean;
import com.cooleshow.teacher.bean.TeacherUserInfo;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.HomeContract;
import com.cooleshow.teacher.databinding.FragmentNewHomeLayoutBinding;
import com.cooleshow.teacher.helper.EventHelper;
import com.cooleshow.teacher.presenter.main.HomePresenter;
import com.cooleshow.teacher.widgets.HomeHotAlbumItemDecoration;
import com.cooleshow.teacher.widgets.HomeHotMusicSheetItemDecoration;
import com.cooleshow.teacher.widgets.dialog.BadgeDesDialog;
import com.cooleshow.teacher.widgets.dialog.HomeCertTipDialog;
import com.cooleshow.usercenter.helper.UserHelper;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.indicator.RoundLinesIndicator;
import com.youth.banner.listener.OnBannerListener;

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

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * Author by pq, Date on 2022/8/8.
 */
public class NewHomeFragment extends BaseMVPFragment<FragmentNewHomeLayoutBinding, HomePresenter> implements HomeContract.HomeView, View.OnClickListener {
    public static final int MAX_HOT_MUSIC_LIST_PAGE = 4;//热门曲目一页最大条数
    public static final int MAX_GOOD_MUSIC_LIST_PAGE = 4;//精品曲目一页最大条数
    public static final String STYLE_TYPE = "STYLE";
    public static final String VIDEON_TYPE = "VIDEO";
    public static final String LIVE_TYPE = "LIVE";
    public static final String MUSIC_TYPE = "MUSIC";
    private long noticeId = 0;
    private HomeLiveAndVideoBean.RecentCoursesBean mRecentCourses;
    private ArrayList<Fragment> fragments = new ArrayList();
    private HomeMenuPageAdapter menuAdapter;
    private HomeHotAlbumAdapter mAlbumAdapter;
    private HomeHotMusicSheetAdapter mHomeHotMusicSheetAdapter;
    private ArrayList<HomeMenuBean.BannerBean> mBannerList = new ArrayList<>();
    private BannerImageAdapter mBannerImageAdapter;
    private int teacherFlag = 0;//教资认证
    private int degreeFlag = 0;//老师学历认证
    private String entryStatus = "";//达人认证状态
    private SmartRefreshLayout refreshLayout;
//    private HomeGoodMusicSheetAdapter mGoodMusicSheetAdapter;

    @Override
    protected void initView(View rootView) {
        Utils.setHeadView(mViewBinding.viewStatusBar, requireContext(), 0);
        refreshLayout = mViewBinding.refreshLayout;
    }

    @Override
    protected void initData() {
        initMenu();
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mViewBinding.rvHotAlbum.setLayoutManager(manager);
        mAlbumAdapter = new HomeHotAlbumAdapter();
        HomeHotAlbumItemDecoration hotAlbumItemDecoration = new HomeHotAlbumItemDecoration(0, 0, 0, SizeUtils.dp2px(14), 0);
        mViewBinding.rvHotAlbum.addItemDecoration(hotAlbumItemDecoration);
        mViewBinding.rvHotAlbum.setAdapter(mAlbumAdapter);


        LinearLayoutManager musicSheetManager = new LinearLayoutManager(getContext());
        musicSheetManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mHomeHotMusicSheetAdapter = new HomeHotMusicSheetAdapter();
        HomeHotMusicSheetItemDecoration itemDecoration = new HomeHotMusicSheetItemDecoration(0, SizeUtils.dp2px(12), 0, SizeUtils.dp2px(12), 0);
        mViewBinding.rvHotTrack.addItemDecoration(itemDecoration);
        mViewBinding.rvHotTrack.setLayoutManager(musicSheetManager);
        mViewBinding.rvHotTrack.setAdapter(mHomeHotMusicSheetAdapter);
        mViewBinding.rvHotTrack.setNestedScrollingEnabled(false);
        mViewBinding.rvHotAlbum.setNestedScrollingEnabled(false);
        mBannerImageAdapter = new BannerImageAdapter<HomeMenuBean.BannerBean>(mBannerList) {
            @Override
            public void onBindView(BannerImageHolder holder, HomeMenuBean.BannerBean data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data.coverImage)
                        .thumbnail(Glide.with(holder.itemView).load(com.cooleshow.base.R.drawable.bg_video_placeholder))//加载成功前显示一个loading的加载
                        //.apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))  //设置图片圆角
                        .into(holder.imageView);
            }
        };
        //banner自带图片轮播的适配器
        mViewBinding.banner.setAdapter(mBannerImageAdapter, true);
        initListener();
    }


    private void initMenu() {
        if (menuAdapter == null) {
            menuAdapter = new HomeMenuPageAdapter(getChildFragmentManager());
            menuAdapter.setFragments(fragments);
        }
        mViewBinding.viewpagerMenu.setAdapter(menuAdapter);
    }

    private void initListener() {
        mViewBinding.viewBadge.setOnClickListener(this);
        mViewBinding.ivScan.setOnClickListener(this);
        mViewBinding.imMessage.setOnClickListener(this);
        mViewBinding.flCourseEnter.setOnClickListener(this);
        mViewBinding.ivCert.setOnClickListener(this);
        mViewBinding.bgAnnouncement.setOnClickListener(this);
        mViewBinding.bgAnnouncement2.setOnClickListener(this);
        mViewBinding.tvHotAlbumMore.setOnClickListener(this);
        mViewBinding.tvHotTrackMore.setOnClickListener(this);
        mViewBinding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            mViewBinding.refreshLayout.finishRefresh();
            presenter.getHotAlbumList();
            presenter.getHotMusicSheetList();
            presenter.queryCountOfUnread();
            presenter.queryLiveAndVideo();
            presenter.appHome();
        });

        mViewBinding.banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIntercept(true) //是否要拦截事件
                .isAutoLoop(true)
                .setIndicatorGravity(IndicatorConfig.Direction.RIGHT)
//                .removeIndicator()
                .setIndicator(new RectangleIndicator(getContext())) //圆形指示器 还支持条形指示器等
                .setOnBannerListener(new OnBannerListener<HomeMenuBean.BannerBean>() {
                    @Override
                    public void OnBannerClick(HomeMenuBean.BannerBean item, int position) {
                        if (!TextUtils.isEmpty(item.linkUrl)) {
                            ARouter.getInstance()
                                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                                    .withString(WebConstants.WEB_URL, item.linkUrl)
                                    .navigation();
                        }
                    }
                });

        mAlbumAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position < mAlbumAdapter.getData().size()) {
                    HomeHotAlbumListBean.RowsBean rowsBean = mAlbumAdapter.getData().get(position);
                    if (rowsBean != null) {
                        WebStartHelper.startHotAlbumDetail(rowsBean.id);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (presenter != null) {
            presenter.queryCountOfUnread();
            presenter.queryLiveAndVideo();
            presenter.appHome();
            //热门专辑
            presenter.getHotAlbumList();
            //热门曲目
            presenter.getHotMusicSheetList();
            //获取领奖状态
            presenter.getAwardStatus();
        }
    }

    @Override
    public void getHomeCountSuccess(HomeCountBean data) {

    }


    private void bindBanner(List<HomeMenuBean.BannerBean> bannerList) {
        if (null == bannerList || bannerList.size() == 0) {
            mViewBinding.banner.setVisibility(View.GONE);
            return;
        }
        mViewBinding.banner.setVisibility(View.VISIBLE);
        mBannerList.clear();
        mBannerList.addAll(bannerList);
        if (mBannerImageAdapter != null) {
            mBannerImageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void helpCenterContentListSuccess(HelpCenterContentBean data) {
        if (data != null && data.rows != null && data.rows.size() > 0) {
            mViewBinding.tvNoticeContent.setText(data.rows.get(0).title);
            mViewBinding.tvNoticeContent2.setText(data.rows.get(0).title);
            noticeId = data.rows.get(0).id;
            if (mRecentCourses != null) {
                //有进入课程提示
                mViewBinding.bgAnnouncement.setVisibility(View.VISIBLE);
                mViewBinding.bgAnnouncement2.setVisibility(View.GONE);
            } else {
                mViewBinding.bgAnnouncement2.setVisibility(View.VISIBLE);
                mViewBinding.bgAnnouncement.setVisibility(View.GONE);
            }
        } else {
            mViewBinding.bgAnnouncement.setVisibility(View.GONE);
            mViewBinding.bgAnnouncement2.setVisibility(View.GONE);
        }
    }


    @Override
    public void queryCountOfUnreadSuccess(List<CountOfUnreadBean> data) {
        if (data != null && data.size() > 0) {
            mViewBinding.viewUnreadMessage.setVisibility(View.VISIBLE);
        } else {
            mViewBinding.viewUnreadMessage.setVisibility(View.GONE);
        }
    }

    @Override
    public void queryLiveAndVideoSuccess(HomeLiveAndVideoBean bean) {
        if (bean == null) {
            return;
        }
        mRecentCourses = bean.recentCourses;
        presenter.helpCenterContentList();
        mViewBinding.ivCourseEnterAnim.clearAnimation();
        mViewBinding.flCourseEnter.setVisibility(mRecentCourses != null ? View.VISIBLE : View.GONE);
        handleEnterAnim();
        if (mRecentCourses != null) {
//            mViewBinding.tvLiveTitle.setText(mRecentCourses.courseGroupName + "即将开始");
            String currentTime = TimeUtils.date2String(new Date(System.currentTimeMillis()), "yyyy-MM-dd");
            String startTime = TimeUtils.date2String(
                    TimeUtils.getDate(mRecentCourses.courseStartTime),
                    "yyyy-MM-dd"
            );
            if (TextUtils.equals(currentTime, startTime)) {
                String timeText =
                        TimeUtils.date2String(TimeUtils.getDate(mRecentCourses.courseStartTime), "HH:mm");
                mViewBinding.tvLiveTime.setText(String.format("今日 %s", timeText));
            } else {
                mViewBinding.tvLiveTime.setText(TimeUtils.date2String(
                        TimeUtils.getDate(mRecentCourses.courseStartTime),
                        "yyyy-MM-dd HH:mm"
                ));
            }
        }
    }

    private void handleEnterAnim() {
        if (mRecentCourses == null) {
            mViewBinding.ivCourseEnterAnim.setVisibility(View.GONE);
            return;
        }
        mViewBinding.ivCourseEnterAnim.setVisibility(View.VISIBLE);
        String assetFolder = "lottie/home/sparring_course/images/";
        int rawRes = R.raw.home_sparring_course_enter;
        if (TextUtils.equals(mRecentCourses.courseType, CourseConstants.LIVE_COURSE)) {
            assetFolder = "lottie/home/live_course/images/";
            rawRes = R.raw.home_live_course_enter;
        }
        if (TextUtils.equals(mRecentCourses.courseType, CourseConstants.OTHER_COURSE)) {
            //陪练课
            assetFolder = "lottie/home/sparring_course/images/";
            rawRes = R.raw.home_sparring_course_enter;
        }
        if (TextUtils.equals(mRecentCourses.courseType, CourseConstants.PIANO_ROOM_COURSE)) {
            //琴房课
            assetFolder = "lottie/home/piano_course/images/";
            rawRes = R.raw.home_piano_course_enter;
        }
        mViewBinding.ivCourseEnterAnim.setImageAssetsFolder(assetFolder);
        mViewBinding.ivCourseEnterAnim.setAnimation(rawRes);
        mViewBinding.ivCourseEnterAnim.playAnimation();

    }

    /**
     * 跳转直播课详情页面
     *
     * @param
     */
    private void gotoCourseDetail(HomeLiveAndVideoBean.RecentCoursesBean bean) {
        if (bean == null) {
            return;
        }

        if (TextUtils.equals(bean.courseType, CourseConstants.LIVE_COURSE)) {
            ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(
                            WebConstants.WEB_URL,
                            String.format(
                                    WebConstants.TEACHER_LIVE_DETAIL_NORMAL_COURSE,
                                    bean.courseGroupId,
                                    bean.courseId
                            )
                    )
                    .navigation();
            return;
        }
        if (TextUtils.equals(bean.courseType, CourseConstants.OTHER_COURSE)) {
            //陪练课
            ARouter.getInstance().build(RouterPath.CourseCenter.SPARRING_COURSE_DETAIL)
                    .withString(CourseConstants.COURSE_ID, String.valueOf(bean.courseId))
                    .withString(CourseConstants.COURSE_GROUP_ID, String.valueOf(bean.courseGroupId))
                    .withString(CourseConstants.STUDENT_ID, bean.studentId.toString())
                    .navigation();
            return;
        }

        if (TextUtils.equals(bean.courseType, CourseConstants.PIANO_ROOM_COURSE)) {
            //琴房课 跳转详情页
            ARouter.getInstance().build(RouterPath.CourseCenter.PIANO_ROOM_COURSE_DETAIL)
                    .withString(CourseConstants.COURSE_ID, String.valueOf(bean.courseId))
                    .navigation();
            return;
        }
    }

    @Override
    public void appHomeSuccess(HomeMenuBean homeMenuBean) {
        if (isDetached()) {
            return;
        }
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
        bindBanner(homeMenuBean.banner);
        if (homeMenuBean != null && homeMenuBean.appMenu != null && homeMenuBean.appMenu.size() > 0) {
            mViewBinding.flTopMenu.setVisibility(View.VISIBLE);
            fragments.clear();
            int count = homeMenuBean.appMenu.size() / 4;
            if (homeMenuBean.appMenu.size() % 4 > 0) {
                count++;
            }

            for (int i = 0; i < count; i++) {
                ArrayList<HomeMenuBean.ItemBean> dataList = new ArrayList<HomeMenuBean.ItemBean>();
                int j = i * 4;
                while (j < homeMenuBean.appMenu.size() && j < (i + 1) * 4) {
                    dataList.add(homeMenuBean.appMenu.get(j));
                    j++;
                }
                fragments.add(HomeMenuFragment.newInstance(dataList));
            }
            menuAdapter.setFragments(fragments);
            menuAdapter.notifyDataSetChanged();
        } else {
            fragments.clear();
            menuAdapter.notifyDataSetChanged();
            mViewBinding.flTopMenu.setVisibility(View.GONE);
        }
    }

    @Override
    public void getHotAlbumListSuccess(HomeHotAlbumListBean homeHotAlbumListBean) {
        if (isDetached() || homeHotAlbumListBean == null) {
            return;
        }
        if (mAlbumAdapter != null) {
            if (homeHotAlbumListBean.rows != null && homeHotAlbumListBean.rows.size() > 0) {
                mViewBinding.clHotAlbum.setVisibility(View.VISIBLE);
                mAlbumAdapter.setNewInstance(homeHotAlbumListBean.rows);
            } else {
                mViewBinding.clHotAlbum.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void getHotMusicSheetListSuccess(HomeHotMusicSheetBean homeHotMusicSheetBean) {
        if (isDetached() || homeHotMusicSheetBean == null) {
            return;
        }
        ArrayList<HomeHotMusicSheetItemBean> itemBeans = new ArrayList<>();
        List<HomeHotMusicSheetBean.RowsBean> rows = homeHotMusicSheetBean.rows;
        if (rows != null && rows.size() > 0) {
            mViewBinding.clHotTrack.setVisibility(View.VISIBLE);
            int pageCount = 0;
            int pageResult = rows.size() % 4;
            if (pageResult == 0) {
                pageCount = rows.size() / 4;
            } else {
                pageCount = (rows.size() / 4) + 1;
            }
            for (int i = 0; i < pageCount; i++) {
                HomeHotMusicSheetItemBean hotMusicSheetItemBean = new HomeHotMusicSheetItemBean();
                for (int k = i * 4; k < rows.size() && k < (i + 1) * 4; k++) {
                    hotMusicSheetItemBean.sheetBeans.add(rows.get(k));
                }
                itemBeans.add(hotMusicSheetItemBean);
            }
            if (mHomeHotMusicSheetAdapter != null) {
                mHomeHotMusicSheetAdapter.setNewInstance(itemBeans);
            }
        } else {
            mViewBinding.clHotTrack.setVisibility(View.GONE);
        }
    }

    @Override
    public void getAwardStatus(AwardStatusBean awardStatusBean) {
        if (isDetached()) {
            return;
        }
        if (awardStatusBean != null && TextUtils.equals(awardStatusBean.check, "1")) {
            showAwardTipDialog();
        }
    }

    private void showAwardTipDialog() {
        if (UserHelper.getShowAwardDialogTipIsCompleted()) {
            return;
        }
        AwardTipDialog awardTipDialog = new AwardTipDialog(getContext());
        awardTipDialog.show();
        UserHelper.setShowAwardDialogTipCompleted();
    }

    public void getTeacherInfoSuccess(TeacherUserInfo teacherUserInfo) {
        if (teacherUserInfo == null) {
            return;
        }
        if (isDetached() || !isAdded()) {
            return;
        }
        if (mViewBinding == null || mViewBinding.tvTeacherName == null) {
            return;
        }
        //昵称
        if (teacherUserInfo != null) {
            String teacherName = UserHelper.getTeacherName(teacherUserInfo.username, teacherUserInfo.userId);
            if (teacherName.length() > 4) {
                mViewBinding.tvTeacherName.setText(teacherName);
            } else {
                mViewBinding.tvTeacherName.setText(String.format("你好，%s", teacherName));
            }
        }
        //星级
        if (teacherUserInfo.starGrade > 0) {
            mViewBinding.ratingBar.setVisibility(View.VISIBLE);
            mViewBinding.ratingBar.setRating(teacherUserInfo.starGrade);
        } else {
            mViewBinding.ratingBar.setVisibility(View.VISIBLE);
        }
        //徽章
        handleBadgeUI(teacherUserInfo.tag);
        //VIP
        mViewBinding.ivVip.setImageResource(teacherUserInfo.isVip == 0 ? com.cooleshow.base.R.drawable.icon_vip_enable : com.cooleshow.base.R.drawable.icon_vip_able);
        //入驻状态
        this.teacherFlag = teacherUserInfo.teacherFlag;
        this.degreeFlag = teacherUserInfo.degreeFlag;
        if (teacherUserInfo.degreeFlag == 1 && teacherUserInfo.teacherFlag == 1) {
            mViewBinding.viewCertRedPoint.setVisibility(View.GONE);
        } else {
            if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
                mViewBinding.viewCertRedPoint.setVisibility(View.VISIBLE);
            } else {
                mViewBinding.viewCertRedPoint.setVisibility(View.GONE);
            }
        }
        this.entryStatus = teacherUserInfo.entryStatus;
        mViewBinding.tvCertFailTip.setVisibility(View.GONE);
        if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_DOING)) {
            //入驻审核中
            mViewBinding.ivCert.setClickable(true);
            mViewBinding.ivCert.setImageResource(R.drawable.icon_home_entry_doing);
        } else if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
            //入驻审核通过
            mViewBinding.ivCert.setClickable(true);
            mViewBinding.ivCert.setImageResource(R.drawable.icon_home_cert_complete);
        } else if (TextUtils.equals(teacherUserInfo.entryStatus, TeacherInfoConstants.ENTRY_STATUS_UNPASS)) {
            //未通过或者未申请
            mViewBinding.tvCertFailTip.setVisibility(View.VISIBLE);
            mViewBinding.ivCert.setClickable(true);
            mViewBinding.ivCert.setImageResource(R.drawable.icon_home_un_certification);
        } else {
            //未申请
            mViewBinding.ivCert.setClickable(true);
            mViewBinding.ivCert.setImageResource(R.drawable.icon_home_un_certification);
        }
    }

    private void handleBadgeUI(String tag) {
        int badgeAbleCount = 0;
        boolean enableStyleBadge = isEnableForBadge(tag, STYLE_TYPE);
        if (enableStyleBadge) {
            badgeAbleCount++;
        }
        boolean enableVideoBadge = isEnableForBadge(tag, VIDEON_TYPE);
        if (enableVideoBadge) {
            badgeAbleCount++;
        }
        boolean enableLiveBadge = isEnableForBadge(tag, LIVE_TYPE);
        if (enableLiveBadge) {
            badgeAbleCount++;
        }

        boolean enableAlbumBadge = isEnableForBadge(tag, MUSIC_TYPE);
        if (enableAlbumBadge) {
            badgeAbleCount++;
        }
        mViewBinding.ivStyleBadge.setImageResource(enableStyleBadge ? R.drawable.icon_style_badge_new_able : R.drawable.icon_style_badge_new_enable);
        mViewBinding.ivVideoBadge.setImageResource(enableVideoBadge ? R.drawable.icon_video_badge_new_able : R.drawable.icon_video_badge_new_enable);
        mViewBinding.ivLiveBadge.setImageResource(enableLiveBadge ? R.drawable.icon_live_badge_new_able : R.drawable.icon_live_badge_new_enable);
        mViewBinding.ivAlbumBadge.setImageResource(enableAlbumBadge ? R.drawable.icon_album_badge_new_able : R.drawable.icon_album_badge_new_enable);
        mViewBinding.tvMedalValue.setText(String.format("%d/4", badgeAbleCount));
    }

    private boolean isEnableForBadge(String tag, String type) {
        if (TextUtils.isEmpty(tag)) {
            return false;
        }
        return tag.contains(type);
    }

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

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

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (UiUtils.isFastClick()) {
            return;
        }
        if (id == R.id.fl_course_enter) {
            if (mRecentCourses != null) {
                gotoCourseDetail(mRecentCourses);
            }
            return;
        }

        if (id == R.id.iv_scan) {
            //二维码扫描
            ARouter.getInstance()
                    .build(RouterPath.BaseCenter.SCAN_QR_CODE)
                    .navigation();
            return;
        }

        if (id == R.id.im_message) {
            //系统消息
            ARouter.getInstance().build(RouterPath.MessageCenter.TEACHER_MESSAGE_MESSAGEBOX)
                    .navigation();
            //事件埋点->消息列表
            EventHelper.addEvent(EventConstants.EVENT_ID_NOTIFICATION_FOR_TEACHER);
            return;
        }
        if (id == R.id.iv_cert) {
            //达人认证
            if (TextUtils.equals(entryStatus, TeacherInfoConstants.ENTRY_STATUS_PASS)) {
                showCertTipDialog();
            } else {
                ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_CERT)
                        .navigation();
            }
            return;
        }
        if (id == R.id.bg_announcement || id == R.id.bg_announcement2) {
            if (noticeId == 0) {
                return;
            }
            //点击单条公告
            ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.HELP_CENTER_DETAIL + noticeId)
                    .navigation();
            return;
        }
        if (id == R.id.tv_hot_album_more) {
            //热门专辑更多
            WebStartHelper.startHotAlbum();
            return;
        }
        if (id == R.id.tv_hot_track_more) {
            //热门曲目更多
            WebStartHelper.startHotMusicSheetList();
            return;
        }
        if (id == R.id.view_badge) {
            showBadgeDialog();
            return;
        }
    }

    private void showCertTipDialog() {
        HomeCertTipDialog homeCertTipDialog = new HomeCertTipDialog(getContext());
        homeCertTipDialog.show();
        homeCertTipDialog.setData(degreeFlag, teacherFlag);
    }

    private void showBadgeDialog() {
        BadgeDesDialog badgeDesDialog = new BadgeDesDialog(getContext());
        badgeDesDialog.show();
    }
}
