package com.hi.music.player.adapter;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.media3.common.util.UnstableApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hi.music.player.MusicApplication;
import com.hi.music.player.R;
import com.hi.music.player.api.onImageColorListener;
import com.hi.music.player.databinding.ItemCategoryListBinding;
import com.hi.music.player.databinding.ItemSingerBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.helper.MyValue;
import com.hi.music.player.javabean.response.ResponsePlayListInfo;
import com.hi.music.player.javabean.response.child.ResponseCategoryListChild;
import com.hi.music.player.javabean.response.child.ResponseSingle;

public class AdapterCategoryList extends BaseAdapter<ResponsePlayListInfo, ItemCategoryListBinding> {


    private String mPageType;

    public void setPageType(String mPageType) {
        this.mPageType = mPageType;
    }

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

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

        ItemCategoryListBinding vb = itemHolder.getVb();
        vb.tvSongName.setText(child.getSongTitle());
        vb.tvDuration.setText(child.getDuration());
        String singerName = child.getSingerName();
        String describe = child.getDescribe();

        switch (mPageType){
            case MyValue.PAGE_TYPE_ALBUM:
                vb.tvSingerName.setText(describe);
                vb.image.setVisibility(View.GONE);
                vb.tvPosition.setVisibility(View.VISIBLE);
                vb.tvPosition.setText(String.valueOf(position));
                break;
            default:
                vb.tvSingerName.setText(singerName);

                vb.image.setVisibility(View.VISIBLE);
                vb.tvPosition.setVisibility(View.GONE);

                Glide.with(MusicApplication.myApplication)
                        .asDrawable()
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(16))))
                        .load(child.getSmallCovert())
                        .placeholder(R.drawable.placeholder)
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                CommonUtils.LogMsg(e.getMessage());
                                return false;
                            }

                            @OptIn(markerClass = UnstableApi.class)
                            @Override
                            public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                return false;
                            }
                        })
                        .into(vb.image);
                break;
        }
        vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeItemClickListener.onClickItemCategoryList(child, itemHolder.getAbsoluteAdapterPosition());
            }
        });
    }


}
