package com.offline.music.playermp3.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.media3.common.MediaItem;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.databinding.ItemPlayListBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.ResponsePlayListInfo;
import com.offline.music.playermp3.media3.MyMediaControllerManager;

public class AdapterPlayList extends BaseAdapter<ResponsePlayListInfo, ItemPlayListBinding> {

    private String mCurVideId;

    private String lastVideId;

    //    private int curMusicPos = 0;
    private MyMediaControllerManager instance = MyMediaControllerManager.getInstance();

    @Override
    protected ItemPlayListBinding getViewBinding(ViewGroup parent) {
        return ItemPlayListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
    }

    public void setCurVideId(String curVideId) {
        lastVideId = mCurVideId;
        this.mCurVideId = curVideId;
        for (int i = 0; i < data.size(); i++) {
            ResponsePlayListInfo listInfo = data.get(i);
            if (listInfo.getVideoId().equals(curVideId)) {
                notifyItemChanged(i);
            }
            if (listInfo.getVideoId().equals(lastVideId)) {
                notifyItemChanged(i);
            }
        }
    }


    public void updateCurMusicAnimation() {
        int curIndex = instance.getCurIndex();
        CommonUtils.LogMsg("--curIndex=" + curIndex);
        notifyItemChanged(curIndex);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ResponsePlayListInfo listInfo = data.get(position);
        VHolder<ItemPlayListBinding> itemHolder = (VHolder<ItemPlayListBinding>) holder;
        ItemPlayListBinding vb = itemHolder.getVb();

        vb.songName.setText(listInfo.getSongTitle());
        String singerName = listInfo.getSingerName();
        String describe = listInfo.getDescribe();
        if (singerName != null && !singerName.isEmpty()) {
            vb.artistName.setText(singerName);
        } else if (describe != null && !describe.isEmpty()) {
            vb.artistName.setText(describe);
        }

        String covert;
        String small = listInfo.getSmallCovert();
        String covert1 = listInfo.getCovert();
        if (small == null || small.isEmpty()) {
            covert = covert1;
        } else {
            covert = small;
        }
        Glide.with(MusicApplication.myApplication)
                .asDrawable()
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(10))))
                .load(covert)
                .placeholder(R.mipmap.im_placeholder)
                .into(vb.imCovert);

        MediaItem currentMediaItem = instance.getCurMediaItem();
        if (currentMediaItem != null && currentMediaItem.mediaId.equals(listInfo.getVideoId())) {
            vb.viewPlaying.setVisibility(View.VISIBLE);
            vb.itemLayout.setBackgroundColor(CommonUtils.getMyColor(R.color.cur_play_music));
            if (instance.getIsPlaying()) {
                vb.viewPlaying.startAnimating();
                lastVideId = listInfo.getVideoId();
                CommonUtils.LogMsg("-------playAnimation " + itemHolder.getAbsoluteAdapterPosition());
            } else {
                vb.viewPlaying.pauseAnimating();
                CommonUtils.LogMsg("-------pauseAnimation " + itemHolder.getAbsoluteAdapterPosition());
            }
        } else {
            vb.viewPlaying.setVisibility(View.GONE);
            vb.itemLayout.setBackgroundColor(CommonUtils.getMyColor(R.color.color_transparent));

        }

        vb.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.LogMsg("-------onClick curMusicPos=" + itemHolder.getAbsoluteAdapterPosition());
                setCurVideId(listInfo.getVideoId());
                instance.playPositionMusic(itemHolder.getAbsoluteAdapterPosition());

            }
        });
    }
}
