package com.cooleshow.teacher.ui.homepage;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.GlideUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.Utils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.HomePagerAdapter;
import com.cooleshow.teacher.bean.TeacherUserInfo;
import com.cooleshow.teacher.constants.TeacherInfoConstants;
import com.cooleshow.teacher.contract.HomePageContract;
import com.cooleshow.teacher.databinding.ActivityHomePageLayoutBinding;
import com.cooleshow.teacher.presenter.homePage.HomePagePresenter;
import com.cooleshow.usercenter.helper.UserHelper;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

/**
 * Author by pq, Date on 2022/5/9.
 * 个人主页
 */
@Route(path = RouterPath.MineCenter.HOME_PAGE)
public class HomePageActivity extends BaseMVPActivity<ActivityHomePageLayoutBinding, HomePagePresenter> implements HomePageContract.HomePageView, View.OnClickListener {
    public static final String[] titles = new String[]{"个人风采", "陪练课", "直播课", "视频课", "乐谱"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private SheetMusicPageFragment mMusicPageFragment;

    @Override
    protected void initView() {
        Utils.setHeadView(viewBinding.viewStatusBar, this, 0);
        viewBinding.ivBack.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(viewBinding.tabLayout, viewBinding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                createTab(tab, titles[position]);
            }
        });
        viewBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    View customView = tab.getCustomView();
                    TextView tv_text = customView.findViewById(R.id.tv_text);
                    tv_text.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_333333));
                    tv_text.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tv_text.setTextSize(17);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    View customView = tab.getCustomView();
                    TextView tv_text = customView.findViewById(R.id.tv_text);
                    tv_text.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_666666));
                    tv_text.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tv_text.setTextSize(15);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        MineStyleFragment mineStyleFragment = new MineStyleFragment();
        SparringCoursePageFragment sparringCoursePageFragment = new SparringCoursePageFragment();
        LiveCoursePageFragment liveCoursePageFragment = new LiveCoursePageFragment();
        VideoCoursePageFragment videoCoursePageFragment = new VideoCoursePageFragment();
        mMusicPageFragment = new SheetMusicPageFragment();
        fragments.add(mineStyleFragment);
        fragments.add(sparringCoursePageFragment);
        fragments.add(liveCoursePageFragment);
        fragments.add(videoCoursePageFragment);
        fragments.add(mMusicPageFragment);
        HomePagerAdapter homePagerAdapter = new HomePagerAdapter(this);
        homePagerAdapter.setData(fragments);
        viewBinding.viewPager.setOffscreenPageLimit(fragments.size());
        viewBinding.viewPager.setAdapter(homePagerAdapter);
        tabLayoutMediator.attach();
    }

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

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

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getTeacherUserInfo();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
            return;
        }
    }

    private TabLayout.Tab createTab(TabLayout.Tab tab, String text) {
        tab.setCustomView(R.layout.view_homepage_tab_layout);
        View customView = tab.getCustomView();
        if (customView != null) {
            TextView tv_text = customView.findViewById(R.id.tv_text);
            tv_text.setText(text);
        }
        return tab;
    }

    /**
     * 获取老师信息成功
     *
     * @param info
     */
    @Override
    public void getTeacherInfoSuccess(TeacherUserInfo info) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        //save达人认证状态和开通直播状态
        UserHelper.saveTeacherCertificationStatus(info.entryStatus);
        UserHelper.saveTeacherOpenLiveFlag(info.liveFlag);
        if (mMusicPageFragment != null) {
            mMusicPageFragment.setMusicPersonCertStatus(info.entryStatus);
        }
        //星级
        if (info.starGrade > 0) {
            viewBinding.ratingBar.setVisibility(View.VISIBLE);
            viewBinding.ratingBar.setRating(info.starGrade);
            viewBinding.tvNoScore.setVisibility(View.GONE);
        } else {
            viewBinding.ratingBar.setVisibility(View.INVISIBLE);
            viewBinding.tvNoScore.setVisibility(View.VISIBLE);
        }
        //徽章信息
        if (!TextUtils.isEmpty(info.tag)) {
            viewBinding.llBadge.setVisibility(View.VISIBLE);
            viewBinding.llBadge.setVipIcon(info.isVip == 1);
            viewBinding.llBadge.setData(info.tag);
        } else {
            if (info.isVip == 1) {
                viewBinding.llBadge.setVipIcon(true);
                viewBinding.llBadge.setVisibility(View.VISIBLE);
            } else {
                viewBinding.llBadge.setVisibility(View.GONE);
            }
        }

        //粉丝数
        viewBinding.tvFansNum.setText(info.fansNum);
        //已上课时数
        viewBinding.tvCourseNumCompleted.setText(info.expTime);
        //头像
        GlideUtils.INSTANCE.loadImage(this, info.heardUrl, viewBinding.ivAvatar, R.drawable.icon_teacher_default_head);
        //昵称
        viewBinding.tvNickname.setText(info.username);
        //subjectName
        try {
            viewBinding.llSubjectContainer.removeAllViews();
            viewBinding.llSubjectContainer2.removeAllViews();
            String[] subjectNames = UiUtils.splitSubjectName(info.subjectName);
            if (subjectNames != null && subjectNames.length > 0) {
                for (int i = 0; i < subjectNames.length; i++) {
                    createSubjectView(subjectNames[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createSubjectView(String text) {
        TextView textView = (TextView) LayoutInflater.from(this).inflate(com.cooleshow.base.R.layout.view_subject_item_layout, viewBinding.llSubjectContainer, false);
        textView.setText(text);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (viewBinding.llSubjectContainer.getChildCount() < 4) {
            if (viewBinding.llSubjectContainer.getChildCount() >= 1) {
                params.setMargins(SizeUtils.dp2px(4), 0, 0, 0);
            }
            viewBinding.llSubjectContainer.addView(textView, params);
        } else {
            viewBinding.llSubjectContainer2.addView(textView, params);
            viewBinding.llSubjectContainer2.setVisibility(View.VISIBLE);
        }
    }
}
