package com.project.ilsaenterprise.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.models.MonitoringListModel;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Objects;

public class MonitoringAdapter extends RecyclerView.Adapter<MonitoringAdapter.MonitoringViewHolder> {
    public ArrayList<MonitoringListModel> monitoringListModels;
    private onItemClickListener clickListener;
    private Context context;

    public interface onItemClickListener{
        void onItemClick(View view, int position);
    }

    public MonitoringAdapter(ArrayList<MonitoringListModel> monitoringListModels, onItemClickListener clickListener, Context context) {
        this.monitoringListModels = monitoringListModels;
        this.clickListener = clickListener;
        this.context = context;
    }

    public class MonitoringViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView imageData, userImage, btnComment;
        public TextView username, district, farm, decription, uploadDate, timestamp, totalSee, totalComment, jabatan1, jabatan2, jabatan3, jabatanNew, jabatan;

        public MonitoringViewHolder(@NonNull View itemView) {
            super(itemView);

            imageData = itemView.findViewById(R.id.imageData);
            userImage = itemView.findViewById(R.id.ivProfile);
            username = itemView.findViewById(R.id.uploaderTxt);
            district = itemView.findViewById(R.id.distrikPost);
            farm = itemView.findViewById(R.id.kebunPost);
            decription = itemView.findViewById(R.id.tvDescription);
            uploadDate = itemView.findViewById(R.id.dayTxt);
            timestamp = itemView.findViewById(R.id.idMonitoring);
            totalSee = itemView.findViewById(R.id.countSee);
            totalComment = itemView.findViewById(R.id.countComment);
            btnComment = itemView.findViewById(R.id.icComment);
            jabatan1 = itemView.findViewById(R.id.jabatanLevel1);
            jabatan2 = itemView.findViewById(R.id.jabatanLevel2);
            jabatan3 = itemView.findViewById(R.id.jabatanLevel3);
            jabatanNew = itemView.findViewById(R.id.jabatanLevel4);
            jabatan = itemView.findViewById(R.id.tvJabatan);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onItemClick(v, getAdapterPosition());
        }

    }

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

        return new MonitoringViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MonitoringViewHolder holder, int position) {
        MonitoringListModel currentItem = monitoringListModels.get(position);

        String imgUrl = currentItem.getImageUrl();
        Picasso.get().load(imgUrl).into(holder.imageData);
        if(currentItem.getImgLink() != null){
            String imgProfile = currentItem.getImgLink();
            RequestOptions options = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL);
            Glide.with(context).load(imgProfile).thumbnail(0.25f).apply(options).into(holder.userImage);
        }
        else
            holder.userImage.setImageResource(R.drawable.user_farmer);
        holder.username.setText(currentItem.getFullName());
        holder.district.setText(currentItem.getDistrict());
        holder.farm.setText(currentItem.getFarm());
        holder.decription.setText(currentItem.getDeskripsi());
        holder.uploadDate.setText(currentItem.getDateUpload());
        holder.timestamp.setText(currentItem.getTimestamp());
        holder.totalSee.setText(String.valueOf(currentItem.getCountSee()));
        holder.totalComment.setText(String.valueOf(currentItem.getCountComment()));
        holder.jabatan.setText(currentItem.getJabatan());
        if(Objects.equals(currentItem.getRole(), "Level 1")){
            holder.jabatan1.setText(currentItem.getRole());
            holder.jabatan1.setVisibility(View.VISIBLE);
        }
        if(Objects.equals(currentItem.getRole(), "Level 2")){
            holder.jabatan2.setText(currentItem.getRole());
            holder.jabatan2.setVisibility(View.VISIBLE);
        }
        if(Objects.equals(currentItem.getRole(), "Level 3")){
            holder.jabatan3.setText(currentItem.getRole());
            holder.jabatan3.setVisibility(View.VISIBLE);
        }
        else
            holder.jabatanNew.setVisibility(View.VISIBLE);

        holder.btnComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "INFO " + currentItem.getImgLink(), Toast.LENGTH_LONG).show();
            }
        });
    }

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

}
