package com.cooleshow.teacher.ui.course;

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

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.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.MineCoursePageAdapter;
import com.cooleshow.teacher.bean.CourseFilterStatusBean;
import com.cooleshow.teacher.bean.QuerySubjectBean;
import com.cooleshow.teacher.constants.CourseConstants;
import com.cooleshow.teacher.contract.MineCourseContract;
import com.cooleshow.teacher.databinding.ActivityMineCourseLayoutBinding;
import com.cooleshow.teacher.presenter.course.MineCoursePresenter;

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

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

/**
 * Author by pq, Date on 2022/4/25.
 */
@Route(path = RouterPath.CourseCenter.TEACHER_MINE_COURSE)
public class MineCourseActivity extends BaseMVPActivity<ActivityMineCourseLayoutBinding, MineCoursePresenter> implements MineCourseContract.MineCourseView {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private SparringCourseFragment mSparringCourseFragment;
    private LiveCourseFragment mLiveCourseFragment;
    private VideoCourseFragment mVideoCourseFragment;
    private PianoRoomCourseFragment mPianoRoomCourseFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

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

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "我的课程");
        viewBinding.radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
                    if (i == R.id.rb_sparring) {
                        viewBinding.viewPager.setCurrentItem(0, false);
                    }
                    if (i == R.id.rb_live_course) {
                        viewBinding.viewPager.setCurrentItem(1, false);
                    }
                    if (i == R.id.rb_video_course) {
                        viewBinding.viewPager.setCurrentItem(2, false);
                    }
                    if (i == R.id.rb_piano_room_course) {
                        viewBinding.viewPager.setCurrentItem(3, false);
                    }
                }
        );
        viewBinding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTableLayoutBar(position);
                BaseMVPFragment fragment = (BaseMVPFragment) mFragments.get(position);
                fragment.refreshData();
            }
        });
    }

    private void changeTableLayoutBar(int position) {
        if (position == 0) {
            viewBinding.rbSparring.setChecked(true);
            viewBinding.rbSparring.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            viewBinding.rbLiveCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbVideoCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbPianoRoomCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
        if (position == 1) {
            viewBinding.rbLiveCourse.setChecked(true);
            viewBinding.rbSparring.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbLiveCourse.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            viewBinding.rbVideoCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbPianoRoomCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
        if (position == 2) {
            viewBinding.rbVideoCourse.setChecked(true);
            viewBinding.rbSparring.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbLiveCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbVideoCourse.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            viewBinding.rbPianoRoomCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
        if (position == 3) {
            viewBinding.rbPianoRoomCourse.setChecked(true);
            viewBinding.rbSparring.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbLiveCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbVideoCourse.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            viewBinding.rbPianoRoomCourse.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        }
    }

    @Override
    public void initData() {
        super.initData();
        mSparringCourseFragment = new SparringCourseFragment();
        mLiveCourseFragment = new LiveCourseFragment();
        mVideoCourseFragment = new VideoCourseFragment();
        mPianoRoomCourseFragment = new PianoRoomCourseFragment();
        mFragments.add(mSparringCourseFragment);
        mFragments.add(mLiveCourseFragment);
        mFragments.add(mVideoCourseFragment);
        mFragments.add(mPianoRoomCourseFragment);
        MineCoursePageAdapter mineCoursePageAdapter = new MineCoursePageAdapter(this);
        mineCoursePageAdapter.setFragments(mFragments);
        viewBinding.viewPager.setAdapter(mineCoursePageAdapter);
        viewBinding.viewPager.setOffscreenPageLimit(mFragments.size());
        presenter.querySubjectItem();
    }

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

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