package com.cooleshow.teacher.ui.live;

import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseActivity;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.CommonPagerAdapter;
import com.cooleshow.teacher.adapter.HomeWorkPagerAdapter;
import com.cooleshow.teacher.bean.LiveListBean;
import com.cooleshow.teacher.contract.LiveListContract;
import com.cooleshow.teacher.databinding.ActivityLiveListLayoutBinding;
import com.cooleshow.teacher.presenter.live.LiveListPresenter;
import com.cooleshow.teacher.ui.work.HomeWorkFragment;
import com.google.android.material.tabs.TabLayout;

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

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * Author by pq, Date on 2022/7/7.
 */
@Route(path = RouterPath.LiveCenter.TEACHER_LIVE_LIST)
public class LiveListActivity extends BaseActivity<ActivityLiveListLayoutBinding> implements View.OnClickListener {
    private static final String[] TITLES = new String[]{"直播中", "已结束"};
    private static final String[] LIVE_STATUS = new String[]{"1", "2"};// 0未开始 1已开始 2已结束
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();

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

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "开启直播");
        viewBinding.toolbarInclude.tvRightText.setVisibility(View.VISIBLE);
        viewBinding.toolbarInclude.tvRightText.setText("创建直播");
        viewBinding.toolbarInclude.tvRightText.setOnClickListener(this);
        tabLayout = viewBinding.tablayout;
        viewPager = viewBinding.viewpager;
        viewPager.setCurrentItem(0);
    }

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

    private void initTabLayoutAndViewPager() {
        tabLayout.removeAllTabs();
        fragments.clear();
        for (int i = 0; i < TITLES.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(TITLES[i]));
            LiveListFragment liveListFragment = LiveListFragment.newInstance(LIVE_STATUS[i]);
            fragments.add(liveListFragment);
        }
        List<String> titles = Arrays.asList(TITLES);
        viewPager.setAdapter(new CommonPagerAdapter(getSupportFragmentManager(), fragments, titles));
        tabLayout.setupWithViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                HomeWorkFragment fragment = (HomeWorkFragment) fragments.get(position);
//                if (fragment != null) {
//                    fragment.queryCourse(false);
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

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

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == com.cooleshow.base.R.id.tv_right_text) {
            ARouter.getInstance().build(RouterPath.LiveCenter.TEACHER_MINE_CREATE_COURSE)
                    .navigation();
        }
    }
}
