package com.younki.maidmatching.services.Adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.GlobalResponse;
import com.younki.maidmatching.services.HTTP.JobApplicantRecruitRequest;
import com.younki.maidmatching.utils.OnUserClickListener;

import java.text.DecimalFormat;

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

public class ApplicantAdapter extends RecyclerView.Adapter<ApplicantAdapter.ViewHolder> {
    ApplicantData[] applicantData;
    Context context;
    OnUserClickListener onUserClickListener;
    JobData jobData;

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.adapter_applicant_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 ApplicantData applicantDataList = applicantData[position];
        DecimalFormat df = new DecimalFormat("#.#####");
        holder.score.setText("Score: "+String.valueOf(df.format(applicantDataList.getScore())));
        holder.name.setText(applicantDataList.getApplicant().getName());

        if (applicantDataList.getApplicant().getRecruit_id() != null) {
            if (applicantDataList.getApplicant().getRecruit_id() != null && applicantDataList.getApplicant().getRecruit_id() != 0 && applicantDataList.getApplicant().getRecruit_id() == applicantDataList.getApplicant().getUser_id()) {
                holder.btn_recruit.setText("Direkrut");
                holder.btn_recruit.setEnabled(false);
//                holder.calling.setVisibility(View.VISIBLE);
            } else {
                holder.btn_recruit.setText("Ditolak");
                holder.btn_recruit.setEnabled(false);
                holder.btn_recruit.setBackground(ContextCompat.getDrawable(context, R.drawable.draw_btn_auth_inverted));
//                holder.btn_recruit.setVisibility(View.GONE);
            }
        }

        holder.btn_recruit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recruit(applicantDataList);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onUserClickListener.onClickedUser(applicantDataList);
            }
        });
    }

    private void recruit(ApplicantData applicantDataList) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading..."); // Set message untuk dialog
        progressDialog.setCancelable(false); // Set apakah dialog bisa di-cancel atau tidak

        progressDialog.show(); // Menampilkan dialog

        Call<GlobalResponse> globalResponseCall = null;

        JobApplicantRecruitRequest jobApplicantRecruitRequest = new JobApplicantRecruitRequest();
        jobApplicantRecruitRequest.setRecruit_id(applicantDataList.getApplicant().getUser_id());

        globalResponseCall = ApiClient.getJobService(context).jobApplicantRecruit(
                jobApplicantRecruitRequest, String.valueOf(applicantDataList.getApplicant().getJob_id())
        );

        globalResponseCall.enqueue(new Callback<GlobalResponse>() {
            @Override
            public void onResponse(Call<GlobalResponse> call, Response<GlobalResponse> response) {
                progressDialog.dismiss();
                if (response.isSuccessful()) {
                    GlobalResponse globalResponse = response.body();

                    if (!globalResponse.isError()) {
                        onUserClickListener.onClickedUser();
                        Toast.makeText(context, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(context, "Gagal merekrut maid.", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<GlobalResponse> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(context, "Throwable "+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView score, name;
        Button btn_recruit;
        ImageView btn_calling;
        LinearLayout calling;
        int userId;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            score = itemView.findViewById(R.id.score);
            name = itemView.findViewById(R.id.name);
            btn_recruit = itemView.findViewById(R.id.btn_recruit);
            btn_calling = itemView.findViewById(R.id.btn_calling);
            calling = itemView.findViewById(R.id.calling);
        }
    }
}
