package com.fun.funnyprank.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.fun.funnyprank.R;
import com.fun.funnyprank.activity.PrewActivity;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.MydataBase;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.RankListBinding;
import com.fun.funnyprank.tools.Mytools;


import java.util.List;

public class RankAdapter extends RecyclerView.Adapter<RankAdapter.RankVH> {
    private Activity mactivity;
    private List<LikeData> mlist;
    private boolean islike = false;

    public RankAdapter(Activity activity) {
        mactivity = activity;

    }

    public void SetData(List<LikeData> list) {
        mlist = list;
    }

    @NonNull
    @Override
    public RankVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RankListBinding binding = RankListBinding.inflate(mactivity.getLayoutInflater(), parent, false);
        return new RankVH(binding);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull RankVH holder, int position) {
        LikeData customdata = mlist.get(position);
        boolean like = customdata.getLike();

        if (position == getItemCount() - 1) {
            holder.getView().setVisibility(View.VISIBLE);
        } else {
            holder.getView().setVisibility(View.GONE);
        }

        if (like) {
            holder.getLike().setBackgroundResource(R.drawable.icon_liek_check);
        } else {
            holder.getLike().setBackgroundResource(R.drawable.icon_liek_uncheck);
        }
        Glide.with(mactivity.getApplicationContext())
                .load(customdata.getPreUrl())
                .placeholder(R.mipmap.icon_logo)
                .into(holder.getImage());

        holder.getLike().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!islike) {
                    islike = true;
                    customdata.setLike(true);
                    holder.getLike().setBackgroundResource(R.drawable.icon_liek_check);
                    Mytools.runIO(new Runnable() {
                        @Override
                        public void run() {
                            MydataBase.getInstance().likeDao().update(customdata);
                        }
                    });
                } else {
                    islike = false;
                    customdata.setLike(false);
                    holder.getLike().setBackgroundResource(R.drawable.icon_liek_uncheck);
                    Mytools.runIO(new Runnable() {
                        @Override
                        public void run() {
                            MydataBase.getInstance().likeDao().update(customdata);
                        }
                    });
                }
            }
        });
        int id = position + 4;
        holder.getId().setText(String.valueOf(id));
        if (customdata.getTitle().length() > 10) {
            String name = customdata.getTitle().substring(0, 10);
            holder.getName().setText(name + "...");
        } else {
            holder.getName().setText(customdata.getTitle());
        }

        holder.getRela().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mactivity, PrewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, customdata.getTitle());
                mactivity.startActivity(intent);
            }
        });

    }


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

    public static class RankVH extends RecyclerView.ViewHolder {
        private TextView id;
        private TextView name;
        private ImageView image;
        private ImageView like;
        private RelativeLayout rela;
        private View view;

        public View getView() {
            return view;
        }

        public TextView getId() {
            return id;
        }

        public TextView getName() {
            return name;
        }

        public ImageView getImage() {
            return image;
        }

        public ImageView getLike() {
            return like;
        }

        public RelativeLayout getRela() {
            return rela;
        }

        public RankVH(@NonNull RankListBinding binding) {
            super(binding.getRoot());
            name = binding.rankItemName;
            like = binding.rankItemLike;
            image = binding.rankItemImage;
            id = binding.rankItemId;
            rela = binding.rankItemRelative;
            view = binding.rankItemView;
        }


    }
}
