package com.cygnusa.hyperm.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
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 com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.SurveyActivity;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.PositionUpdateListener;
import com.cygnusa.hyperm.wrappers.Survey;

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

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

    private Context context;

    private PositionUpdateListener listener;
    private ArrayList<Survey> SURVEY_LIST = new ArrayList<>();


    public SurveyAdapter(Context context, ArrayList<Survey> items,
                         PositionUpdateListener l) {
        this.SURVEY_LIST = items;
        this.context = context;

        this.listener = l;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feedback_item, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Survey assessmentList = (Survey) v.getTag();
                SurveyActivity.ASSESSMENT = assessmentList;

                if (listener != null) {
                    listener.onPositionUpdate(SURVEY_LIST.indexOf(assessmentList));
                }

                if (assessmentList.getStatus().trim().equalsIgnoreCase("7")||assessmentList.getStatus().trim().equalsIgnoreCase("3")) {
                    context.startActivity(new Intent(context, SurveyActivity.class));
                }else {
                    GlobalData.showToast(context,"Survey expired", Toast.LENGTH_SHORT).show();
                }

            }
        });
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Survey item = SURVEY_LIST.get(position);
        holder.view.setTag(item);
        holder.title.setText(item.getAssessmentname());
        holder.title.setSelected(true);
        holder.publishedDate.setText("Published Date : "+dateFormate(item.getPublisheddate()));
        holder.status.setText(item.getStatusname());


        if (item.getStatus().trim().equalsIgnoreCase("3")) {
            holder.status.setTextColor(Color.parseColor("#1D7210"));
        }else if(item.getStatus().trim().equalsIgnoreCase("8")){
            holder.status.setTextColor(context.getResources().getColor(R.color.material_red900));
        }  else{
            holder.status.setTextColor(Color.parseColor("#B5B5B5"));
        }


        holder.description.setText(item.getDescription());
        holder.title.setSelected(true);
        Glide.with(context).load(item.getAssessmentimage_url())
                .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                .apply(new RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.placeholder)).into(holder.image);
    }
    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");
        String date = format.format(newDate);


        return date;
    }

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

    public void add(Survey item, int position) {
        SURVEY_LIST.add(position, item);
        notifyItemInserted(position);
    }

    public void add(Survey item) {
        SURVEY_LIST.add(item);
        notifyItemInserted(SURVEY_LIST.size());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image, logo;
        TextView title, description,status,publishedDate;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.text_title);
            status = itemView.findViewById(R.id.text_status);
            publishedDate = itemView.findViewById(R.id.text_date);

            description = itemView.findViewById(R.id.text_desc);



           // logo = itemView.findViewById(R.id.logo);
        }


    }
}
