package com.fun.airhornprank.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.bumptech.glide.Glide;
import com.fun.airhornprank.R;
import com.fun.airhornprank.activity.PreViewActivity;
import com.fun.airhornprank.data.MyDataBase;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.CustomListBinding;
import com.fun.airhornprank.tool.Mytools;
import com.fun.airhornprank.tool.StaticValue;

import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomVH> {
    private Activity mactivity;
    private List<Mydata> mlist;
    private boolean islike = false;
    private boolean show;

    public CustomAdapter(Activity activity) {
        mactivity = activity;

    }

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

    public void showDelete(boolean isShow) {
        show = isShow;
        notifyDataSetChanged();
    }

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

    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull CustomVH holder, int position) {
        Mydata customdata = mlist.get(position);
        boolean like = customdata.getLike();
        if (like) {
            holder.getLike().setBackgroundResource(R.drawable.icon_like);
        } else {
            holder.getLike().setBackgroundResource(R.drawable.icon_custom_unlike);
        }
        int milliseconds = (int) (customdata.getTime() % 1000) / 10;
        int seconds = (int) (customdata.getTime() / 1000) % 60;
        int minutes = (int) (customdata.getTime() / (1000 * 60)) % 60;
        holder.getTime().setText(String.format("%d : %02d : %02d", minutes, seconds, milliseconds));

        if (show) {
            holder.getDelete().setVisibility(View.VISIBLE);
        } else {
            holder.getDelete().setVisibility(View.GONE);
        }
        holder.getDelete().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mactivity)
                        .setTitle("Tips")
                        .setMessage("Are you sure you want to delete" + " " + customdata.getTitle())
                        .setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Mytools.runIO(new Runnable() {
                                    @Override
                                    public void run() {
                                        MyDataBase.getInstance().likeDAO().deleteCutsom(customdata.id);
                                    }
                                });
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();

            }
        });

        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_like);
                    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_custom_unlike);
                    Mytools.runIO(new Runnable() {
                        @Override
                        public void run() {
                            MyDataBase.getInstance().likeDAO().update(customdata);
                        }
                    });
                }
            }
        });

        holder.getImage().setImageResource(R.mipmap.save_image);
        if (customdata.getTitle().length() > 3) {
            String name = customdata.getTitle().substring(0, 3);
            holder.getName().setText(name + "...");
        } else {
            holder.getName().setText(customdata.getTitle());
        }
        holder.getCustomcons().setBackgroundResource(R.drawable.custom_item_bg);
        holder.getImage().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mactivity, PreViewActivity.class);
                intent.putExtra(StaticValue.KEY_TITLE, customdata.getTitle());
                mactivity.startActivity(intent);
            }
        });
    }


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

    public static class CustomVH extends RecyclerView.ViewHolder {
        private ConstraintLayout customcons;
        private TextView name, time;
        private ImageView image;
        private ImageView like;
        private ConstraintLayout delete;

        public CustomVH(@NonNull CustomListBinding binding) {
            super(binding.getRoot());
            name = binding.customName;
            customcons = binding.customCons;
            time = binding.customTime;
            like = binding.customLike;
            image = binding.customImage;
            delete = binding.customItemDelete;

        }

        public ConstraintLayout getDelete() {
            return delete;
        }

        public ConstraintLayout getCustomcons() {
            return customcons;
        }

        public TextView getName() {
            return name;
        }

        public TextView getTime() {
            return time;
        }

        public ImageView getImage() {
            return image;
        }

        public ImageView getLike() {
            return like;
        }
    }
}
