package com.younki.maidmatching.services.Adapter;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
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 android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.GlobalResponse;
import com.younki.maidmatching.services.SharedPreference.Preferences;
import com.younki.maidmatching.ui.OwnerDialog;
import com.younki.maidmatching.ui.ShowDetailJobDialog;
import com.younki.maidmatching.utils.OnUserClickListener;

import java.text.NumberFormat;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

    JobData[] jobData;
    Context context;
    OnUserClickListener onUserClickListener;

    public JobMaidAdapter(JobData[] jobData, Context context, OnUserClickListener onUserClickListener) {
        this.jobData = jobData;
        this.context = context;
        this.onUserClickListener = onUserClickListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Context context = holder.itemView.getContext();
        final JobData jobDataList = jobData[position];
        NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("id", "ID"));

        holder.salary.setText(formatter.format(Integer.valueOf(jobDataList.getSalary())));
        holder.name.setText(jobDataList.getName());
        holder.applicant.setText("Pelamar: "+jobDataList.getApplicant());
        holder.recommended_age.setText("Rentang Umur: "+jobDataList.getRecommended_age());
        Picasso.get().load(ApiClient.getBaseUrl()+jobDataList.getImage()).into(holder.job_image);

        if (jobDataList.getRecruit_id() != null && jobDataList.getRecruit_id() != 0) {
            if (jobDataList.getRecruit_id() == Preferences.getUserId(context)) {
                CardView cardView = holder.itemView.findViewById(R.id.cardView);
                cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.c_10));
                holder.salary.setText("Anda Direkrut");
                holder.calling.setVisibility(View.VISIBLE);
            } else {
                CardView cardView = holder.itemView.findViewById(R.id.cardView);
                cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.c_10_inverted));
                holder.salary.setText("Anda Ditolak");
            }
        }

        if (jobDataList.getUser_kyc() == 2) {
            holder.verified.setVisibility(View.VISIBLE);
        } else {
            holder.verified.setVisibility(View.GONE);
        }

        holder.btn_calling.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OwnerDialog showDetailJobDialog = new OwnerDialog(v.getContext(), jobDataList.getUser_id());
                showDetailJobDialog.setCancelable(true);
                showDetailJobDialog.getWindow().setBackgroundDrawable(new ColorDrawable(v.getResources().getColor(android.R.color.transparent)));
                showDetailJobDialog.show();
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onUserClickListener != null) {
                    ShowDetailJobDialog showDetailJobDialog = new ShowDetailJobDialog(v.getContext(), jobDataList);
                    showDetailJobDialog.setCancelable(true);
                    showDetailJobDialog.getWindow().setBackgroundDrawable(new ColorDrawable(v.getResources().getColor(android.R.color.transparent)));
                    showDetailJobDialog.show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return jobData.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView salary, name, applicant, recommended_age, verified;
        ImageView job_image, btn_calling;
        LinearLayout calling;
        int userId;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            salary = itemView.findViewById(R.id.salary);
            name = itemView.findViewById(R.id.name);
            applicant = itemView.findViewById(R.id.applicant);
            verified = itemView.findViewById(R.id.verified);
            recommended_age = itemView.findViewById(R.id.recommended_age);
            job_image = itemView.findViewById(R.id.job_image);
            btn_calling = itemView.findViewById(R.id.btn_calling);
            calling = itemView.findViewById(R.id.calling);
        }
    }
}
