package com.renitha.Adapter;

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
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.Model.SetterViewComplaintDetails;
import com.renitha.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewComplaintAdapter extends  RecyclerView.Adapter<RecyclerViewComplaintAdapter.ViewHolder> {
    static Context context;
    private LayoutInflater inflater;
    public static List<SetterViewComplaintDetails> getDataAdapter;

    public RecyclerViewComplaintAdapter(List<SetterViewComplaintDetails> getDataAdapter) {
        this.getDataAdapter = getDataAdapter;
    }

    public RecyclerViewComplaintAdapter(List<SetterViewComplaintDetails> 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_view_complaint_history_cardview, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position)
    {
        final int pos = position;
        final SetterViewComplaintDetails getDataAdapter1 = getDataAdapter.get(position);
       // userConatctNo.setText(Html.fromHtml("<u>"+response.body().getUser_contact()+"</u>"));
        holder.userName.setText("Engineer Name : "+getDataAdapter1.getUser_name());
        holder.userConatctNo.setText("Engineer Contact : "+ Html.fromHtml("<u>"+getDataAdapter1.getUser_contact()+"</u>"));
        holder.userJobDate.setText("Job Date : "+getDataAdapter1.getJob_date());
        holder.userJobTime.setText("Job Time : "+getDataAdapter1.getJob_time());
        holder.userJobTimeTotal.setText("Total Time : "+getDataAdapter1.getJob_tot_time());
        holder.userFaultDetails.setText("Fault Identified : "+getDataAdapter1.getFault_details());
        holder.userServicePerformed.setText("Service Performed : "+getDataAdapter1.getService_performed());
        holder.userRateUs.setText("Rate Our service : "+getDataAdapter1.getRate_our());
        holder.userCustomerRemark.setText("Remark : "+getDataAdapter1.getCust_remark());
        for(int i=0;i <getDataAdapter1.getPhotoList().size();i++)
        {
            final String str = getDataAdapter1.getPhotoList().get(i).getPhoto_url();
            TextView valueTV = new TextView(context);
            valueTV.setText(Html.fromHtml("<u>Download</u>"));
            valueTV.setPadding(10,5,0,5);
            valueTV.setTextColor(Color.BLACK);
            valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            valueTV.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    DownloadManager downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                    Uri uri = Uri.parse(str);

                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setTitle("Renitha Downloaded Image");
                    request.setDescription("Downloading");
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"renitha_"+System.currentTimeMillis() + ".png");
                    downloadmanager.enqueue(request);
                }
            });
            holder.linearLayout.addView(valueTV);
        }
        holder.userConatctNo.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);
                }
            }
        });
    }

    @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 userName,userConatctNo,userEmail,userJobDate,userJobTime,userJobTimeTotal,userFaultDetails;
        public TextView userServicePerformed,userRateUs,userCustomerRemark;
       // public ImageView imageViewCall;
        public LinearLayout linearLayout;
        public ViewHolder(View itemView)
        {
            super(itemView);

            userName          = (TextView) itemView.findViewById(R.id.userName);
            userConatctNo          = (TextView) itemView.findViewById(R.id.userConatctNo);
            userEmail           = (TextView) itemView.findViewById(R.id.userEmail);
            userJobDate         = (TextView) itemView.findViewById(R.id.userJobDate);
            userJobTime       = (TextView) itemView.findViewById(R.id.userJobTime);
            userJobTimeTotal         = (TextView) itemView.findViewById(R.id.userJobTimeTotal);

            userFaultDetails       = (TextView) itemView.findViewById(R.id.userFaultDetails);
            userServicePerformed         = (TextView) itemView.findViewById(R.id.userServicePerformed);
            userRateUs       = (TextView) itemView.findViewById(R.id.userRateUs);
            userCustomerRemark         = (TextView) itemView.findViewById(R.id.userCustomerRemark);
            linearLayout         = (LinearLayout) itemView.findViewById(R.id.linearLayout);

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

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