package com.cygnusa.hyperm.adapter;

import android.annotation.SuppressLint;
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 com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.cygnusa.hyperm.AudioViewActivity;
import com.cygnusa.hyperm.PdfViewActivity;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.VideoViewActivity;
import com.cygnusa.hyperm.wrappers.ContentFile;
import com.cygnusa.hyperm.wrappers.MyContent;
import com.cygnusa.hyperm.wrappers.MyTopic;
import com.cygnusa.hyperm.wrappers.TrainingCategory;

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

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

    private ArrayList<MyContent> items;
    private Context context;
    private MyTopic TOPIC;
    private TrainingCategory myTraining;

    public ContentAdapter(Context context, ArrayList<MyContent> items, MyTopic myTopic,TrainingCategory myTraining) {
        this.items = items;
        this.TOPIC = myTopic;
        this.myTraining=myTraining;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyContent item = (MyContent) v.getTag();
                ContentFile file = getContentFile(item);
                assert file != null;
                if (file.getFiletypeid().trim().equalsIgnoreCase("1")) {
                    PdfViewActivity.CONTENT = item;
                    PdfViewActivity.TOPIC = TOPIC;
                    PdfViewActivity.TRAINING_ID=myTraining;
                    Intent i=new Intent(context,PdfViewActivity.class);
                    i.putExtra("type","content");
                    context.startActivity(i);
                } else if (file.getFiletypeid().trim().equalsIgnoreCase("2")) {
                    VideoViewActivity.CONTENT = item;
                    VideoViewActivity.TOPIC = TOPIC;
                    VideoViewActivity.TRAINING_ID= myTraining;
                    Intent i=new Intent(context,VideoViewActivity.class);
                    i.putExtra("type","content");
                    context.startActivity(i);

                } else if (file.getFiletypeid().trim().equalsIgnoreCase("3")) {
                    AudioViewActivity.CONTENT = item;
                    AudioViewActivity.TOPIC = TOPIC;
                    context.startActivity(new Intent(context, AudioViewActivity.class));
                }
            }
        });
        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyContent item = items.get(position);
        ContentFile contentFile = getContentFile(item);

        holder.view.setTag(item);
        holder.title.setText(item.getTitle());
        holder.description.setText(item.getDescription());
        holder.title.setSelected(true);
        try {
            holder.publishedDate.setText("Published On: " + dateFormate(item.getUploaddate()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        assert contentFile != null;
        if (contentFile.getFiletypeid().trim().equalsIgnoreCase("1")) {
            holder.logo.setImageResource(R.mipmap.pdf);
        } else if (contentFile.getFiletypeid().trim().equalsIgnoreCase("2")) {
            holder.logo.setImageResource(R.mipmap.video_icon);
        } else if (contentFile.getFiletypeid().trim().equalsIgnoreCase("3")) {
            holder.logo.setImageResource(R.mipmap.audio_icon);
        }

        holder.title.setText(item.getTitle());
        if (item.getStatus().trim().equalsIgnoreCase("0")) {
            holder.image.setBorderColor(Color.parseColor("#B5B5B5"));
        } else if (item.getStatus().trim().equalsIgnoreCase("1")) {
            holder.image.setBorderColor(Color.parseColor("#F98315"));
        } else if (item.getStatus().trim().equalsIgnoreCase("2")) {
            holder.image.setBorderColor(Color.parseColor("#1D7210"));
        }

        if (contentFile != null) {
            Glide.with(context).load(contentFile.getThumb_fileurl())
                    .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;
    }

    private ContentFile getContentFile(MyContent item) {
        if (item.getContent_files() == null) {
            return null;
        }

        String language = context.getSharedPreferences("LANGUAGE", Context.MODE_PRIVATE).getString("DEFAULT", "en_us");
        for (ContentFile file : item.getContent_files()) {
            if (file.getLanguage_code().trim().equalsIgnoreCase(language.trim())) {
                return file;
            }
        }
        return item.getContent_files().get(0);
    }

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

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

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

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

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

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
            description = itemView.findViewById(R.id.description);
            publishedDate = itemView.findViewById(R.id.publishedDate);
            //   statusBar = itemView.findViewById(R.id.statusBar);
            logo = itemView.findViewById(R.id.logo);
        }

    }
}
