package com.example.cloudmusic.adapter;

import static androidx.core.content.ContextCompat.startActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.cloudmusic.R;
import com.example.cloudmusic.activity.SongListActivity;
import com.example.cloudmusic.been.SongListBeen;

import java.util.List;

public class SongListAdapter extends RecyclerView.Adapter<SongListAdapter.InnerHolder> {

    private final List<SongListBeen> mList;
    private final Context mContext;
    private Handler mHandler;

    public SongListAdapter(List<SongListBeen> list, Context context) {
        mList = list;
        mContext = context;
    }

    public boolean isGroupHead(int position){
        if(position == 0){
            return true;
        }else{
            String currentGroupName = getGroupName(position);
            String preGroupName = getGroupName(position - 1);
            if (currentGroupName.equals(preGroupName)){
                return false;
            }else
                return true;
        }
    }

    @NonNull
    @Override
    public SongListAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new InnerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.horizon_scroll_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull SongListAdapter.InnerHolder holder, int position) {
        holder.setData(mList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    public String getGroupName(int position){
        return mList.get(position).getName();
    }

    public class InnerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final TextView tv_people_number;
        private final TextView tv_product;
        private final ImageView item;
        private final TextView title;
        private SongListBeen been;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            tv_people_number = itemView.findViewById(R.id.people_number);
            tv_product = itemView.findViewById(R.id.sing_product);
            item = itemView.findViewById(R.id.horizon_item);
            title = itemView.findViewById(R.id.horizon_title);

            itemView.setOnClickListener(this);
        }

        @SuppressLint("HandlerLeak")
        public void setData(SongListBeen been) {
            this.been = been;
            tv_people_number.setText(Integer.parseInt(been.getPlayCount()) / 10000 + "万");
            try {
                RequestOptions requestOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL);

                Glide.with(mContext)
                        .load(been.getPicUr())
                        .apply(requestOptions)
                        .placeholder(R.drawable.cloudmusic_gray)
                        .error(R.drawable.cloudmusic_white)
                        .into(item);
            } catch (Exception e) {
                e.printStackTrace();
                // 处理异常，例如显示默认图片或者提示用户
            }
            title.setText(been.getName());
            tv_product.setText(been.getName().substring(0,5));
            mHandler = new Handler(){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case 1: item.setImageBitmap((Bitmap) msg.obj);
                    }
                }
            };
        }
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext, SongListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("name",been.getName());
            bundle.putString("id",been.getId());
            intent.putExtras(bundle);
            startActivity(mContext,intent,bundle);
        }





//        private void getIma(String picUr,int what) {
//            if (!picUr.isEmpty()) {
//                try {
//                    URL url = new URL(picUr);
//                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                    connection.setDoInput(true);
//                    connection.connect();
//                    InputStream input = connection.getInputStream();
//                    Bitmap myBitmap = BitmapFactory.decodeStream(input);
//                    input.close();
//                    Message msg = new Message();
//                    msg.what = what;
//                    msg.obj = myBitmap;
//                    mHandler.sendMessage(msg);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

    }
}

