package com.renitha.Adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.renitha.Activity.ViewComplaintInfoActivity;
import com.renitha.Model.SetterComplaintDetails;
import com.renitha.R;

import java.util.ArrayList;

public class RecyclerCustomerPendingAdapter extends  RecyclerView.Adapter<RecyclerCustomerPendingAdapter.ViewHolder> {
    static Context context;
    private LayoutInflater inflater;
    public static ArrayList<SetterComplaintDetails> getDataAdapter;

    public RecyclerCustomerPendingAdapter(ArrayList<SetterComplaintDetails> getDataAdapter) {
        this.getDataAdapter = getDataAdapter;
    }

    public RecyclerCustomerPendingAdapter(ArrayList<SetterComplaintDetails> getDataAdapter, Context context) {
        super();
        inflater = LayoutInflater.from(context);
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_complaint_list_cardview, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position)
    {
        final int pos = position;
        final SetterComplaintDetails getDataAdapter1 = getDataAdapter.get(position);

        holder.tvServiceProductName.setText(getDataAdapter1.getProduct_id()+"( Model: "+getDataAdapter1.getModel()+" )");
        holder.tvServicePersonName.setText("Assigned to : "+getDataAdapter1.getUser_name());
        holder.tvComplaintId.setText("#"+getDataAdapter1.getTicket_no());
        holder.tvComplaintDate.setText(getDataAdapter1.getTicket_date());
        if(!getDataAdapter1.getCompleted_date().equals("")) {
            holder.tvCompletedDate.setText("Completed on " + getDataAdapter1.getCompleted_date());
        }
        if(getDataAdapter1.getTicket_priority().equals("Low"))
        {
            holder.tvStatus.setBackgroundColor(Color.parseColor("#008000"));
        }
        else if(getDataAdapter1.getTicket_priority().equals("Medium"))
        {
            holder.tvStatus.setBackgroundColor(Color.parseColor("#FFD433"));
        }
        else if(getDataAdapter1.getTicket_priority().equals("High"))
        {
            holder.tvStatus.setBackgroundColor(Color.parseColor("#FF0000"));
        }
        holder.tvStatus.setText(getDataAdapter1.getTicket_priority());

        if(getDataAdapter1.getFlag().equals("0")||getDataAdapter1.getFlag().equals("1"))
        {
            holder.linear_view.setBackgroundColor(Color.parseColor("#FFD433"));
        }
        else if(getDataAdapter1.getFlag().equals("2")||getDataAdapter1.getFlag().equals("3")) {
            holder.linear_view.setBackgroundColor(Color.parseColor("#008000"));
        }
        else if(getDataAdapter1.getFlag().equals("4")||getDataAdapter1.getFlag().equals("5")) {
            holder.linear_view.setBackgroundColor(Color.parseColor("#FF0000"));
        }


        if(!getDataAdapter1.getUser_contact().equals(""))
        {
            holder.imageViewCall.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getDataAdapter1.getUser_contact()));
                    if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                    {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 143);
                    }
                    else {
                        v.getContext().startActivity(intent);
                    }
                }
            });
        }

        holder.btnCustomerInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(), ViewComplaintInfoActivity.class);
                i.putExtra("id",getDataAdapter1.getTbl_ticket_entry_id());
                v.getContext().startActivity(i);
            }
        });
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // public TextView NameTextView;
        public TextView tvServiceProductName,tvServicePersonName,tvComplaintId,tvComplaintDate,tvStatus,tvCompletedDate,btnCustomerInfo;
        public ImageView imageViewCall;
        public View linear_view;
        public ViewHolder(View itemView)
        {
            super(itemView);

            tvServiceProductName     = (TextView) itemView.findViewById(R.id.tvServiceProductName);
            tvServicePersonName     = (TextView) itemView.findViewById(R.id.tvServicePersonName);
            tvComplaintId           = (TextView) itemView.findViewById(R.id.tvComplaintId);
            tvComplaintDate         = (TextView) itemView.findViewById(R.id.tvComplaintDate);
            tvCompletedDate         = (TextView) itemView.findViewById(R.id.tvCompletedDate);
            tvStatus                = (TextView) itemView.findViewById(R.id.tvStatus);
            btnCustomerInfo         = (TextView) itemView.findViewById(R.id.btnCustomerInfo);

            imageViewCall           = (ImageView) itemView.findViewById(R.id.imageViewCall);

            linear_view             = (View) itemView.findViewById(R.id.view_status);
        }

        @Override
        public void onClick(View view) {
            Log.d("Onclick", "onClick " + getPosition() + " ");
        }
    }
}
