package com.app.simplitend.patientprofile.setuproutine.mvvm;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

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

import com.app.simplitend.R;
import com.app.simplitend.databinding.RoutineViewholderBinding;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

public class RoutineAdapter extends ListAdapter<RoutineDetails, RoutineAdapter.RoutineViewHolder> {

    private static final String TAG = "RoutineAdapter";

    private static final DiffUtil.ItemCallback<RoutineDetails> DIFF_UTIL = new DiffUtil.ItemCallback<RoutineDetails>() {
        @Override
        public boolean areItemsTheSame(@NonNull RoutineDetails oldItem, @NonNull RoutineDetails newItem) {
            // TODO: 10-07-2023
            return false;
        }

        @Override
        public boolean areContentsTheSame(@NonNull RoutineDetails oldItem, @NonNull RoutineDetails newItem) {
            // TODO: 10-07-2023
            return false;
        }
    };

    private DeleteClickListener deleteClickListener;
    private ClickListener clickListener;

    private CheckClickListener checkClickListener;

    private volatile Date selected_date;

    public RoutineAdapter() {
        super(DIFF_UTIL);
    }

    public void setDeleteClickListener(DeleteClickListener deleteClickListener) {
        this.deleteClickListener = deleteClickListener;
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setCheckClickListener(CheckClickListener checkClickListener) {
        this.checkClickListener = checkClickListener;
    }

    public void setSelected_date(Date selected_date) {
        this.selected_date = selected_date;
    }

    public Date getSelected_date() {
        return selected_date;
    }

    @NonNull
    @Override
    public RoutineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RoutineViewholderBinding binding = RoutineViewholderBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new RoutineViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RoutineViewHolder holder, int position) {
        holder.setData(getItem(position), position);

        holder.binding.delete.setOnClickListener(v -> {
            if (deleteClickListener != null)
                deleteClickListener.onDelete(getItem(position), position);
        });

        holder.binding.card.setOnClickListener(v -> {
            if (clickListener != null) clickListener.onClick(getItem(position), position);
        });

        holder.binding.check.setOnClickListener(v -> {
            if (checkClickListener != null)
                checkClickListener.onCheckClick(getItem(position), position);
        });

    }

    public static class RoutineViewHolder extends RecyclerView.ViewHolder {

        RoutineViewholderBinding binding;

        public RoutineViewHolder(RoutineViewholderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(RoutineDetails routine, int position) {

            binding.routineName.setText(routine.routine_title);
            binding.description.setText(routine.routine_description);

            // converting time format from hh:mm:ss to hh:mm s

            SimpleDateFormat input_sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
            SimpleDateFormat output_sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());

            String start_time, end_time;

            try {
                Date start_date = input_sdf.parse(routine.routine_start_time);
                start_time = output_sdf.format(Objects.requireNonNull(start_date)).replace(".", "").toUpperCase();

                Date end_date = input_sdf.parse(routine.routine_end_time);
                end_time = output_sdf.format(Objects.requireNonNull(end_date)).replace(".", "").toUpperCase();
            } catch (Exception e) {
                start_time = routine.routine_start_time;
                end_time = routine.routine_end_time;
            }

            String time_slot = start_time + " - " + end_time;
            binding.timeSlot.setText(time_slot);

            // static in format of hh a
            String start_slot = start_time.substring(0, 2) + " " + start_time.substring(start_time.length() - 2);
            binding.startTimeStatic.setText(start_slot);

            String end_slot = end_time.substring(0, 2) + " " + end_time.substring(end_time.length() - 2);
            binding.endTimeStatic.setText(end_slot);

            if (routine.routine_marked != null){
                /*
                    This routine is done for some day.
                    New, checking if this routine done is done today or day before it
                 */

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

                try {
                    Date routine_date = sdf.parse(routine.routine_marked);
                    if (routine_date == null)
                        throw new Exception("Cannot parse routine marked date from Backend");

                    RoutineAdapter routineAdapter = (RoutineAdapter) getBindingAdapter();

                    if (routineAdapter == null) throw new Exception();

                    Date selected_date = routineAdapter.getSelected_date();
                    Calendar selected_calendar = Calendar.getInstance();
                    selected_calendar.setTime(selected_date);

                    Calendar routine_calendar = Calendar.getInstance();
                    routine_calendar.setTime(routine_date);

                    Log.d(TAG, "setData: " + routine_calendar.get(Calendar.DAY_OF_MONTH) + " " + selected_calendar.get(Calendar.DAY_OF_MONTH));

                    if (routine_calendar.get(Calendar.DAY_OF_MONTH) == selected_calendar.get(Calendar.DAY_OF_MONTH)){
                        // it is the same day on which routine is checked
                        // as routine done date is equal to selected_tab date
                        binding.check.setImageResource(R.drawable.ic_check_green);
                    }else{
                        binding.check.setImageResource(R.drawable.ic_un_check);
                    }

                } catch (Exception e) {
                    Log.e(TAG, "setData: ", e);
                    Toast.makeText(itemView.getContext(), "Couldn't mark it done.", Toast.LENGTH_SHORT).show();
                    binding.check.setImageResource(R.drawable.ic_un_check);
                }
            }else{
                binding.check.setImageResource(R.drawable.ic_un_check);
            }

        }

    }

    // interfaces

    @FunctionalInterface
    public interface DeleteClickListener {
        void onDelete(RoutineDetails routineDetails, int position);
    }

    @FunctionalInterface
    public interface ClickListener {
        void onClick(RoutineDetails routineDetails, int position);
    }

    @FunctionalInterface
    public interface CheckClickListener {
        void onCheckClick(RoutineDetails routineDetails, int position);
    }

}
