package com.xunao.effectdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
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.AudioDemandAdapter;
import com.xunao.effectdemo.adapter.VideoDemandAdapter;
import com.xunao.effectdemo.fragment.AudioDemandFragment;
import com.xunao.effectdemo.fragment.VideoDemandFragment;

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

public class AudioActivity extends FragmentActivity {

    private ViewPager viewPager;

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

    private List<Fragment> list;
    private AudioDemandAdapter adapter;

    private int selectedNum = 0;
    private TextView tvNumAudio;
    private int length;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);
        length = 0;
        for(String[] data : datas){
            length = data.length + length;
        }
        initView();
    }

    private void initView(){
        tvNumAudio = findViewById(R.id.tv_num_audio);
        tvNumAudio.setText("探索 0/" + length);
        viewPager = findViewById(R.id.view_pager_audio);
        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        for(String[] data : datas){
            AudioDemandFragment audioDemandFragment = new AudioDemandFragment();
            Bundle bundle = new Bundle();
            bundle.putStringArray("data",data);
            audioDemandFragment.setArguments(bundle);
            list.add(audioDemandFragment);
        }
        adapter = new AudioDemandAdapter(getSupportFragmentManager(), list);
//        viewPager.setAdapter(new VideoDemandAdapter(this, datas));
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(datas.length);
        viewPager.setCurrentItem(0);

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

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

    public void addSelectedNum() {

        selectedNum = selectedNum + 1;
        if(selectedNum == length){
            tvNumAudio.setText("完成");
        }else{
            tvNumAudio.setText("探索 "+selectedNum+"/" + length);
        }
    }
}