package com.lc.myapplication.security.mysoundstwo.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

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

import com.lc.myapplication.security.mysoundstwo.R;
import com.lc.myapplication.security.mysoundstwo.activity.PreviewActivity;
import com.lc.myapplication.security.mysoundstwo.databinding.RadioRecycleBinding;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.util.List;

public class RadioAdapter extends RecyclerView.Adapter<RadioAdapter.RadioVH> {
    private List<ScaryPrankLikeEntity> scaryPrankLikeEntity;
    private boolean islike;
    private Activity myac;
    private boolean isdelete;
    public RadioAdapter(Activity activity,List<ScaryPrankLikeEntity> entities,boolean delete){
        myac = activity;
        scaryPrankLikeEntity = entities;
        isdelete = delete;
    }
    @NonNull
    @Override
    public RadioVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RadioRecycleBinding radioRecycleBinding = RadioRecycleBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        return new RadioVH(radioRecycleBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RadioVH holder, int position) {
        ScaryPrankLikeEntity entity = scaryPrankLikeEntity.get(position);
        holder.radioRecycleBinding.radioViewName.setText(entity.getName());
        holder.radioRecycleBinding.radioViewImage.setBackgroundResource(R.mipmap.creat_image);
        String image = String.valueOf(R.mipmap.creat_image);
        entity.setImage(image);
       // Log.d("gettime","-----------"+entity.getTime());
        holder.radioRecycleBinding.radioViewTime.setText(entity.getTime());
        islike = entity.getSoundslike();
        if (islike){
            holder.radioRecycleBinding.radioViewLike.setBackgroundResource(R.drawable.like_bg);
        }else {
            holder.radioRecycleBinding.radioViewLike.setBackgroundResource(R.drawable.unlike_bg);
        }
        //Log.d("like1","lie"+entity.getSoundslike());
        holder.radioRecycleBinding.radioViewLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!islike){
                    holder.radioRecycleBinding.radioViewLike.setBackgroundResource(R.drawable.like_bg);
                    entity.setSoundslike(!islike);
                    islike = true;
                    //Log.d("like2","lie"+entity.getSoundslike());
                }else {
                    holder.radioRecycleBinding.radioViewLike.setBackgroundResource(R.drawable.unlike_bg);
                    islike = false;
                    entity.setSoundslike(islike);
                    //Log.d("like2","lie"+entity.getSoundslike());
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(entity);
                    }
                });
            }
        });
        if (isdelete){
            holder.radioRecycleBinding.radioViewDelete.setVisibility(View.VISIBLE);
            holder.radioRecycleBinding.radioViewDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyTool.RunIO(new Runnable() {
                        @Override
                        public void run() {
                            ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().deletelike(entity.getName());
                            Toast.makeText(myac,"Delete success",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
        }
        String bg = String.valueOf(R.color.white);
        holder.radioRecycleBinding.radioViewLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(myac, PreviewActivity.class);
                intent.putExtra(MyValue.DATA_NAME,entity.getName());
                intent.putExtra(MyValue.DATA_URLMP3,entity.getMp3url());
                intent.putExtra(MyValue.DATA_BG,bg);
                intent.putExtra(MyValue.DATA_IMAGE,"c");
                myac.startActivity(intent);
                //Log.d("mentityp","-----------fin"+entity.getImage());
            }
        });
    }

    @Override
    public int getItemCount() {
        return scaryPrankLikeEntity.size();
    }

    public class RadioVH extends RecyclerView.ViewHolder {
        private RadioRecycleBinding radioRecycleBinding;
        public RadioVH(@NonNull RadioRecycleBinding rb) {
            super(rb.getRoot());
            radioRecycleBinding = rb;
        }
    }
}
