package com.xunao.effectdemo.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.xunao.effectdemo.R;
import com.xunao.effectdemo.adapter.VideoDemandAdapter;
import com.xunao.effectdemo.fragment.VideoDemandFragment;

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

/**
 * 视频点播
 */
public class VideoDemandActivity extends FragmentActivity {

    private ViewPager viewPager;
    private String[] datas = {"a", "b", "c", "d", "e", "f"};

    private List<Fragment> list;
    private VideoDemandAdapter adapter;

    private int selectedNum = 0;
    private TextView tvNum;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_demand);
        initView();
    }

    private void initView(){
        tvNum = findViewById(R.id.tv_num);
        tvNum.setText("探索 0/"+datas.length);
        viewPager = findViewById(R.id.view_pager);
        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        for(String data : datas){
            VideoDemandFragment videoDemandFragment = new VideoDemandFragment();
            Bundle bundle = new Bundle();
            bundle.putString("data",data);
            videoDemandFragment.setArguments(bundle);
            list.add(videoDemandFragment);
        }
        adapter = new VideoDemandAdapter(getSupportFragmentManager(), list);
//        viewPager.setAdapter(new VideoDemandAdapter(this, datas));
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(datas.length);
        viewPager.setCurrentItem(0);

        // 上一个
        ImageView tvPrevious = findViewById(R.id.tv_previous);
        tvPrevious.setOnClickListener(v -> {
            int current = viewPager.getCurrentItem();
            if(current > 0){
                viewPager.setCurrentItem(current - 1);
            }
        });

        // 下一个
        ImageView tvNext = findViewById(R.id.tv_next);
        tvNext.setOnClickListener(v -> {
            int current = viewPager.getCurrentItem();
            if(current < datas.length - 1){
                viewPager.setCurrentItem(current + 1);
            }
        });
    }

    public void addSelectedNum() {
        selectedNum = selectedNum + 1;
        if(selectedNum == datas.length){
            tvNum.setText("完成");
        }else{
            tvNum.setText("探索 "+selectedNum+"/"+datas.length);
        }
    }
}