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 android.widget.Toast;

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

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

import com.renitha.R;

import java.util.ArrayList;

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

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

    public RecyclerUserComplaintCompletedAdapter(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_user_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.tvHospitalName.setText(getDataAdapter1.getHospital_name());
        holder.tvCustomerName.setText(getDataAdapter1.getCust_name());
        holder.tvComplaintId.setText("#"+getDataAdapter1.getTicket_no());
        holder.tvComplaintDate.setText(getDataAdapter1.getTicket_date());
        holder.tvCustomerAddress.setText(getDataAdapter1.getHospital_address());


        holder.linear_view.setBackgroundColor(Color.parseColor("#008000"));

        if(!getDataAdapter1.getCompleted_date().equals("")) {
            holder.tvCompletedDate.setText("Completed on " + getDataAdapter1.getCompleted_date());
        }
        if(!getDataAdapter1.getCust_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.getCust_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 tvHospitalName,tvCustomerName,tvComplaintId,tvComplaintDate,tvStatus,tvCompletedDate,tvCustomerAddress;
        public TextView btnCustomerAccept,btnCustomerDone,btnCustomerReject,btnCustomerInfo;
        public ImageView imageViewCall;
        public View linear_view;
        public ViewHolder(View itemView)
        {
            super(itemView);

            tvHospitalName          = (TextView) itemView.findViewById(R.id.tvHospitalName);
            tvCustomerName          = (TextView) itemView.findViewById(R.id.tvCustomerName);
            tvComplaintId           = (TextView) itemView.findViewById(R.id.tvComplaintId);
            tvComplaintDate         = (TextView) itemView.findViewById(R.id.tvComplaintDate);
            tvCustomerAddress       = (TextView) itemView.findViewById(R.id.tvCustomerAddress);
            tvCompletedDate         = (TextView) itemView.findViewById(R.id.tvCompletedDate);

            btnCustomerAccept       = (TextView) itemView.findViewById(R.id.btnCustomerAccept);
            btnCustomerDone         = (TextView) itemView.findViewById(R.id.btnCustomerDone);
            btnCustomerReject       = (TextView) itemView.findViewById(R.id.btnCustomerReject);
            btnCustomerInfo         = (TextView) itemView.findViewById(R.id.btnCustomerInfo);

            tvStatus                = (TextView) itemView.findViewById(R.id.tvStatus);

            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() + " ");
        }
    }

}
