package com.cygnusa.hyperm.adapter;

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

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

import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.Bulletin;
import com.cygnusa.hyperm.wrappers.LearningPath;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


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

    private ArrayList<LearningPath> items;
    private Context context;
    private Bulletin TOPIC;

    public LearningPathAdapter(Context context, ArrayList<LearningPath> item) {

        this.items = item;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.learning_path_item, parent, false);

        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LearningPath item = items.get(position);
        holder.view.setTag(item);
        holder.title.setText(item.assessmentname);
        holder.title.setSelected(true);
        holder.score.setText(item.highscore + "%");
        holder.title.setSelected(true);

        try {
            //   char levelChar=item.lvl_name.toString().charAt(0);
            holder.Date.setText("Date:  " + dateFormate(item.examtime));
            holder.learningLevelName.setText(item.lvl_name.toString().substring(0, 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int score = Integer.parseInt(item.highscore);
        if (score > 80) {
            holder.image.setImageResource(R.drawable.asset13);
        } else {
            holder.image.setImageResource(R.drawable.asset12);
        }
//        if (item.getLvl_id().equalsIgnoreCase("3")) {
//            holder.image.setImageResource(R.drawable.asset13);
//        }else if (item.getLvl_id().equalsIgnoreCase("2")) {
//            holder.image.setImageResource(R.drawable.asset12);
//        } else if (item.getLvl_id().equalsIgnoreCase("1")) {
//            holder.image.setImageResource(R.drawable.asset11);
//        } else {
//            holder.image.setImageResource(R.drawable.asset14);
//        }

        if (item.getLvl_id().equalsIgnoreCase("1") || item.getLvl_id().equalsIgnoreCase("2")) {

            if (item.getLevel_status().equalsIgnoreCase("0")) {
                holder.image.setImageResource(R.drawable.l_full_grey);
            } else if (item.getLevel_status().equalsIgnoreCase("2")) {
                holder.image.setImageResource(R.drawable.asset12);
            } else if (item.getLevel_status().equalsIgnoreCase("3")) {
                holder.image.setImageResource(R.drawable.l_full_green);
            } else {
                holder.image.setImageResource(R.drawable.l_full_grey);
            }
        } else if (item.getLvl_id().equalsIgnoreCase("3") || item.getLvl_id().equalsIgnoreCase("4")) {
            if (item.getLevel_status().equalsIgnoreCase("0")) {
                holder.image.setImageResource(R.drawable.l_full_grey);
            } else if (item.getLevel_status().equalsIgnoreCase("2")) {
                holder.image.setImageResource(R.drawable.l_green_red);
            } else if (item.getLevel_status().equalsIgnoreCase("3")) {
                holder.image.setImageResource(R.drawable.l_full_green);
            } else if (item.getLevel_status().equalsIgnoreCase("6")) {
                holder.image.setImageResource(R.drawable.l_red_grey);
            } else if (item.getLevel_status().equalsIgnoreCase("7")) {
                holder.image.setImageResource(R.drawable.l_green_grey);
            } else if (item.getLevel_status().equalsIgnoreCase("4")) {
                holder.image.setImageResource(R.drawable.l_green_grey);
            } else {
                holder.image.setImageResource(R.drawable.l_full_grey);
            }
        }
    }


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

    public void add(LearningPath item, int position) {
        items.add(position, item);
        notifyItemInserted(position);
    }

    public void add(LearningPath item) {
        items.add(item);
        notifyItemInserted(items.size());
    }

    public void remove(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title, score, Date, learningLevelName;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
            learningLevelName = itemView.findViewById(R.id.learning_path_name);
            Date = itemView.findViewById(R.id.date);
            score = itemView.findViewById(R.id.score);

        }

    }

    public String dateFormate(String dte) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = null;
        try {
            newDate = format.parse(dte);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        format = new SimpleDateFormat("dd-MMM-yyyy");


        return format.format(newDate);
    }
}

