package com.younki.maidmatching.services.Adapter;

import static android.content.ContentValues.TAG;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.Html;
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.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

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

import java.text.DecimalFormat;

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

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

    public RecentApplicantAdapter(ApplicantData[] applicantData, Context context, OnUserClickListener onUserClickListener) {
        this.applicantData = applicantData;
        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_applicant_recent_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];
        holder.score.setText("Lowongan: "+String.valueOf(applicantDataList.getApplicant().getJob_name()));
        holder.name.setText(applicantDataList.getApplicant().getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getJob(applicantDataList.getApplicant().getJob_id());
            }
        });
    }

    private void getJob(int jobId) {
        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<JobResponse> jobResponseCall = ApiClient.getJobService(context).getJobDetail(String.valueOf(jobId));

        jobResponseCall.enqueue(new Callback<JobResponse>() {
            @Override
            public void onResponse(Call<JobResponse> call, Response<JobResponse> response) {
                progressDialog.dismiss();

                if (response.isSuccessful()){
                    JobResponse jobResponse = response.body();

                    JobData jobData = new JobData(
                            jobResponse.getData().get(0).getId(),
                            jobResponse.getData().get(0).getUser_id(),
                            jobResponse.getData().get(0).getName(),
                            jobResponse.getData().get(0).getDescription(),
                            jobResponse.getData().get(0).getImage(),
                            jobResponse.getData().get(0).getSalary(),
                            jobResponse.getData().get(0).getAddress(),
                            jobResponse.getData().get(0).getLocation(),
                            jobResponse.getData().get(0).getRecommended_age(),
                            jobResponse.getData().get(0).getSkills(),
                            jobResponse.getData().get(0).getParameters(),
                            jobResponse.getData().get(0).getApplicant(),
                            jobResponse.getData().get(0).getApplicants(),
                            jobResponse.getData().get(0).getRecruit_id(),
                            jobResponse.getData().get(0).getAccommodation(),
                            jobResponse.getData().get(0).getWorking_days(),
                            jobResponse.getData().get(0).getWorking_hours(),
                            jobResponse.getData().get(0).getUser_kyc()
                    );

                    onUserClickListener.onClickedUserData(jobData);

                } else {
                    Toast.makeText(context, "Gagal Mengambil Data", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<JobResponse> call, Throwable t) {
                progressDialog.dismiss();

                String errorMessage = t.getMessage();
                Log.e(TAG, "onFailure: "+t.getMessage());
            }
        });
    }

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

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