package com.jachdev.consumerprotection.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jachdev.commonlibs.utils.Helper;
import com.jachdev.commonlibs.widget.CustomImageView;
import com.jachdev.commonlibs.widget.CustomTextView;
import com.jachdev.consumerprotection.R;
import com.jachdev.consumerprotection.data.BotMessage;
import com.jachdev.consumerprotection.data.DietPlan;
import com.jachdev.consumerprotection.data.Exercise;
import com.jachdev.consumerprotection.data.FirebasePredictionData;
import com.jachdev.consumerprotection.data.Notification;
import com.jachdev.consumerprotection.data.enums.MessageType;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by prabuddhaon 7/30/2021.
 */
public class CommonAdaptor<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int VIEW_TYPE_NOTIFICATION = R.layout.item_notification;
    public static final int VIEW_TYPE_DIET_PLAN = R.layout.item_diet_plan;
    public static final int VIEW_TYPE_EXE_PLAN = R.layout.item_exe;

    private Context context;
    private CommonAdaptorCallback callback;
    private final List<T> data;
    private int viewType = 0;

    public CommonAdaptor(Context context, List<T> data, CommonAdaptorCallback callback) {
        viewType = MessageType.SENDER.getLayout();
        this.context = context;
        this.data = data;
        this.callback = callback;
    }

    @Override
    public @NotNull RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        if(viewType == MessageType.SENDER.getLayout()){
            return new SenderVH(view);
        } else if(viewType == MessageType.RECEIVER.getLayout()){
            return new ReceiverVH(view);
        } else if(viewType == VIEW_TYPE_DIET_PLAN){
            return new DietPlanVH(view);
        } else if(viewType == VIEW_TYPE_EXE_PLAN){
            return new ExeVH(view);
        } else {
            return new NotificationVH(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        T t = data.get(position);

        if (data.get(position) instanceof BotMessage){
            BotMessage message = (BotMessage) data.get(position);

            if(message.getMessageType() == MessageType.SENDER){

                SenderVH vh = (SenderVH) holder;
                vh.onBind((BotMessage) t, position);

            } else{

                ReceiverVH vh = (ReceiverVH) holder;
                vh.onBind((BotMessage) t, position);

            }
        } else {

            switch (viewType){
                case VIEW_TYPE_NOTIFICATION:
                    NotificationVH nvh = (NotificationVH) holder;
                    nvh.onBind((Notification) t, position);
                    break;
                case VIEW_TYPE_DIET_PLAN:
                    DietPlanVH dvh = (DietPlanVH) holder;
                    dvh.onBind((DietPlan) t, position);
                    break;
                case VIEW_TYPE_EXE_PLAN:
                    ExeVH evh = (ExeVH) holder;
                    evh.onBind((Exercise) t, position);
                    break;
            }
        }
    }

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

    @Override
    public int getItemViewType(int position) {

        if (data.get(position) instanceof BotMessage){
            BotMessage message = (BotMessage) data.get(position);

            return message.getMessageType().getLayout();
        }else{

            return viewType;
        }
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public void add(T t) {
        data.add(t);
        notifyDataSetChanged();
    }

    public void remove(T t) {
        data.remove(t);
        notifyDataSetChanged();
    }

    public void update(int position, T t) {
        data.add(position, t);
        notifyDataSetChanged();
    }

    public void setItems(List<T> t) {
        if (t == null) return;

        this.data.clear();
        this.data.addAll(t);
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return data;
    }

    public void clearData() {
        if(data!=null) {
            data.clear();
            notifyDataSetChanged();
        }
    }

    public class SenderVH extends RecyclerView.ViewHolder {

        Context context;
        @BindView(R.id.tv_message)
        CustomTextView tvMessage;
        int position;

        public SenderVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            context = itemView.getContext();
        }

        public void onBind(BotMessage item, int position) {
            this.position = position;

            tvMessage.setAnyText(item.getMessage());
        }
    }

    public class ReceiverVH extends RecyclerView.ViewHolder {

        Context context;
        @BindView(R.id.tv_message)
        CustomTextView tvMessage;
        int position;

        public ReceiverVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            context = itemView.getContext();
        }

        public void onBind(BotMessage item, int position) {
            this.position = position;

            tvMessage.setAnyText(item.getMessage());
        }
    }

    public class NotificationVH extends RecyclerView.ViewHolder {

//        Context context;
//        @BindView(R.id.tvTitle)
//        CustomTextView tvTitle;
//        @BindView(R.id.tvContent)
//        CustomTextView tvContent;
//        int position;

        public NotificationVH(View itemView) {
            super(itemView);
//            ButterKnife.bind(this, itemView);
//
//            context = itemView.getContext();
        }

        public void onBind(Notification item, int position) {
//            this.position = position;
//
//            tvTitle.setAnyText(item.getTitle());
//            tvContent.setAnyText(item.getDescription());
        }
    }

    public class DietPlanVH extends RecyclerView.ViewHolder {

        Context context;
        @BindView(R.id.tvHeader)
        CustomTextView tvHeader;
        @BindView(R.id.tvDescription)
        CustomTextView tvDescription;
        @BindView(R.id.tvFoodType)
        CustomTextView tvFoodType;
        @BindView(R.id.ivPreview)
        ImageView ivPreview;
        int position;

        public DietPlanVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            context = itemView.getContext();
        }

        public void onBind(DietPlan item, int position) {
            this.position = position;

            tvHeader.setAnyText(item.getName());
            tvDescription.setAnyText(item.getDescription());
            tvFoodType.setAnyText(item.getFoodType());
            Helper.loadImage(ivPreview, item.getUrl());
        }
    }

    public class ExeVH extends RecyclerView.ViewHolder {

        Context context;
        @BindView(R.id.tvHeader)
        CustomTextView tvHeader;
        @BindView(R.id.tvDescription)
        CustomTextView tvDescription;
        @BindView(R.id.tvFoodType)
        CustomTextView tvFoodType;
        @BindView(R.id.ivPreview)
        ImageView ivPreview;
        int position;

        public ExeVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            context = itemView.getContext();
        }

        public void onBind(Exercise item, int position) {
            this.position = position;

            tvHeader.setAnyText(item.getName());
            tvDescription.setAnyText(item.getDescription());
//            tvFoodType.setAnyText(item.getFoodType());
            Helper.loadImage(ivPreview, item.getUrl());
        }
    }

    public interface CommonAdaptorCallback {

        void onView(int viewType, int position);

    }
}
