package com.cooleshow.student.ui.course;

import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

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.ui.fragment.BaseMVPFragment;
import com.cooleshow.student.adapter.MineCoursePagerAdapter;
import com.cooleshow.student.bean.QuerySubjectBean;
import com.cooleshow.student.contract.MineCourseContract;
import com.cooleshow.student.databinding.ActivityMineCourseBinding;
import com.cooleshow.student.presenter.course.MineCoursePresenter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/30 9:21
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.CourseCenter.TEACHER_MINE_COURSE)
public class MineCourseActivity extends BaseMVPActivity<ActivityMineCourseBinding, MineCoursePresenter> implements MineCourseContract.MineCourseView {


    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private List<String> titles = new ArrayList<String>(Arrays.asList("陪练课", "直播课", "视频课", "琴房课"));
    private List<BaseMVPFragment> fragments = new ArrayList<>();
    private MinePracticeCourseFragment mMinePracticeCourseFragment;
    private LiveCourseFragment mLiveCourseFragment;
    private VideoCourseFragment mVideoCourseFragment;
    private PianoRoomCourseFragment mPianoRoomCourseFragment;

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "我的课程");
        tabLayout = viewBinding.tablayout;
        viewPager = viewBinding.viewpager;
        viewPager.setCurrentItem(0);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }

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

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

    @Override
    public void initData() {
        super.initData();
        initTabLayoutAndViewPager();
        presenter.querySubjectItem();
    }

    private void initTabLayoutAndViewPager() {
        mMinePracticeCourseFragment = MinePracticeCourseFragment.newInstance();
        mLiveCourseFragment = LiveCourseFragment.newInstance();
        mVideoCourseFragment = VideoCourseFragment.newInstance();
        mPianoRoomCourseFragment = new PianoRoomCourseFragment();
        fragments.add(mMinePracticeCourseFragment);
        fragments.add(mLiveCourseFragment);
        fragments.add(mVideoCourseFragment);
        fragments.add(mPianoRoomCourseFragment);
        MineCoursePagerAdapter mineCoursePageAdapter = new MineCoursePagerAdapter(this);
        mineCoursePageAdapter.setFragments(fragments);
        viewPager.setAdapter(mineCoursePageAdapter);
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            //这里需要根据position修改tab的样式和文字等
            tab.setText(titles.get(position));
            tab.select();
        }).attach();
        viewPager.setOffscreenPageLimit(fragments.size());

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                BaseMVPFragment fragment = fragments.get(position);
                fragment.refreshData();
            }
        });
    }

    @Override
    public void querySubjectItemSuccess(List<QuerySubjectBean> data) {
        if (mMinePracticeCourseFragment != null) {
            mMinePracticeCourseFragment.querySubjectItemSuccess(data);
        }
        if (mLiveCourseFragment != null) {
            mLiveCourseFragment.querySubjectItemSuccess(data);
        }
        if (mVideoCourseFragment != null) {
            mVideoCourseFragment.querySubjectItemSuccess(data);
        }
        if (mPianoRoomCourseFragment != null) {
            mPianoRoomCourseFragment.querySubjectItemSuccess(data);
        }
    }
}
