package com.cooleshow.metronome.adapter;

import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cooleshow.metronome.R;
import com.cooleshow.metronome.bean.BeatBean;

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

import androidx.annotation.NonNull;

/**
 * Author by pq, Date on 2022/9/19.
 */
public class MetronomeAdapter extends BaseQuickAdapter<BeatBean, BaseViewHolder> {
    public static final int DEFAULT_SELECT_POSITION=-1;
    private int selectPosition = DEFAULT_SELECT_POSITION;
    private int count = 0;

    public MetronomeAdapter() {
        super(R.layout.item_beat_symbol_layout);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, BeatBean bean) {
        View view_first = holder.getView(R.id.view_first);
        View view_other = holder.getView(R.id.view_other);
        int position = holder.getLayoutPosition();
        view_first.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
        view_other.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
        if (position == selectPosition) {
            view_first.setBackgroundResource(R.drawable.shape_2dc7aa_beat_select);
            view_other.setBackgroundResource(R.drawable.shape_2dc7aa_beat_select);
        } else {
            view_first.setBackgroundResource(R.drawable.shape_2dc7aa_beat_normal);
            view_other.setBackgroundResource(R.drawable.shape_2dc7aa_beat_normal);
        }
    }

    public void setCount(int count) {
        this.count = count;
        Log.i("pq", "count:" + count);
        ArrayList<BeatBean> arrayList = new ArrayList();
        for (int i = 0; i < count; i++) {
            arrayList.add(new BeatBean());
        }
        setList(arrayList);
    }


    public void notifyData(int selectPosition) {
//        Log.i("pq", "position:" + selectPosition);
        int defItemCount = getDefItemCount();
//        Log.i("pq", "defItemCount:" + defItemCount);
        this.selectPosition = selectPosition != -1 ? selectPosition % defItemCount : -1;
//        Log.i("pq", "selectPosition:" + this.selectPosition);
        notifyDataSetChanged();
    }
}
