package com.dowerx.quack.fragment;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

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

import com.dowerx.quack.R;
import com.dowerx.quack.activity.SwitcherActivity;
import com.dowerx.quack.databinding.FragmentPostBinding;
import com.dowerx.quack.model.Post;
import com.dowerx.quack.service.AuthService;
import com.dowerx.quack.service.PostService;

import java.util.List;

public class PostRecyclerViewAdapter extends RecyclerView.Adapter<PostRecyclerViewAdapter.ViewHolder> {

    private final List<Post> mValues;
    private final SwitcherActivity activity;

    public PostRecyclerViewAdapter(List<Post> items, SwitcherActivity activity) {
        this.mValues = items;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentPostBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.username.setText(holder.mItem.getUser().getUsername());
        holder.username.setOnClickListener(v->{
            activity.setUser(holder.mItem.getUser().getId());
            activity.findViewById(R.id.nav_profile).performClick();
        });

        holder.content.setText(holder.mItem.getContent());

        holder.like.setText((holder.mItem.isLiked() ? "Liked " : "Likes ") + holder.mItem.getLikes());
        holder.like.setBackgroundColor(holder.mItem.isLiked() ? Color.rgb(36, 166, 71) : Color.rgb(173, 33, 61));
        holder.like.setOnClickListener(v -> {
            PostService.ToggleLike task = new PostService.ToggleLike(
                    ()->{
                        holder.mItem.setLikes(holder.mItem.getLikes()+1);
                        holder.like.setText("Liked " + holder.mItem.getLikes());
                        holder.like.setBackgroundColor(Color.rgb(36, 166, 71));
                        return null;
                    },
                    ()->{
                        holder.mItem.setLikes(holder.mItem.getLikes()-1);
                        holder.like.setText("Likes " + holder.mItem.getLikes());
                        holder.like.setBackgroundColor(Color.rgb(173, 33, 61));
                        return null;
                    },
                    v.getContext());
            task.execute(holder.mItem.getId(), AuthService.getId());
        });

        if (!AuthService.getId().equals(holder.mItem.getUser().getId())) {
            holder.remove.setAlpha(0.0f);
            holder.remove.setClickable(false);
        } else {
            holder.remove.setOnClickListener(v -> {
                PostService.RemovePost task = new PostService.RemovePost(()-> {
                    this.mValues.remove(position);
                    this.notifyItemRemoved(position);
                    this.notifyItemRangeChanged(position, mValues.size());
                    return null;
                }, v.getContext());
                task.execute(holder.mItem.getId());
            });
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView username;
        public final TextView content;
        public final Button like;
        public final Button remove;
        public Post mItem;

        public ViewHolder(@NonNull FragmentPostBinding binding) {
            super(binding.getRoot());
            username = binding.username;
            content = binding.content;
            like = binding.like;
            remove = binding.remove;
        }

        @NonNull
        @Override
        public String toString() {
            return super.toString() + " '" + content.getText() + "'";
        }
    }
}