package com.daya.live_teaching.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.daya.live_teaching.R;

/**
 * Description:
 * Copyright  : Copyright (c) 2019
 * Company    : 大雅乐盟
 * Author     : r
 * Date       : 2020/6/18 15:29
 */
public class MetronomeAdapter extends BaseAdapter {


    private Context mContext;
    private int     count         = 4;
    private int     selectPositon = 0;
    private double  delayMillis   = 0d;

    public MetronomeAdapter(Context context) {
        this.mContext = context;
    }


    public void setData(int count, double delayMillis) {
        selectPositon=0;
        this.delayMillis = delayMillis;
        this.count = count;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_metronome_list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ivMetronome = (ImageView) convertView.findViewById(R.id.iv_metronome);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position == selectPositon) {
            if (position == 0) {
                viewHolder.ivMetronome.setImageResource(R.drawable.bg_play_metronome_green_dots_shape);
            } else {
                viewHolder.ivMetronome.setImageResource(R.drawable.bg_play_metronome_white_dots_shape);
            }
            ViewHolder finalViewHolder = viewHolder;
            if (delayMillis > 0) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finalViewHolder.ivMetronome.setImageResource(R.drawable.bg_play_metronome_gray_dots_shape);
                    }
                }, (long) (delayMillis - 100));
            }

        } else {
            viewHolder.ivMetronome.setImageResource(R.drawable.bg_play_metronome_gray_dots_shape);
        }
        return convertView;
    }

    public void notifyPositon(int selectPositon) {
        this.selectPositon = selectPositon;
        notifyDataSetChanged();
    }


    public class ViewHolder {
        ImageView ivMetronome;
    }
}
