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.Color;
import android.graphics.drawable.Drawable;
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.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.ui.owner.OwnerJobFragment;
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 JobOwnerAdapter extends RecyclerView.Adapter<JobOwnerAdapter.ViewHolder> {

    JobData[] jobData;
    Context context;
    OnUserClickListener onUserClickListener;

    public JobOwnerAdapter(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_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) {
            CardView cardView = holder.itemView.findViewById(R.id.cardView);
            cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.c_10));
            holder.salary.setText("Selesai Merekrut");
        } else {
            CardView cardView = holder.itemView.findViewById(R.id.cardView);
            cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.white));
        }

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(context)
                        .setTitle("Hapus Lowongan")
                        .setMessage("Apakah Anda yakin ingin menghapus lowongan ini?")
                        .setPositiveButton("Ya", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteJob(context, jobDataList.getId());
                            }
                        })
                        .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onUserClickListener != null) {
                    onUserClickListener.onClickedUserData(jobDataList);
                }
            }
        });
    }

    private void deleteJob(Context context, int id) {
        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;

        globalResponseCall = ApiClient.getJobService(context).deleteJob(String.valueOf(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()) {
                        Toast.makeText(context, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    onUserClickListener.onClickedUser();
                } else {
                    Toast.makeText(context, "Gagal Menghapus Lowongan.", 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 jobData.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView salary, name, applicant, recommended_age;
        ImageView job_image, btn_delete;
        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);
            recommended_age = itemView.findViewById(R.id.recommended_age);
            job_image = itemView.findViewById(R.id.job_image);
            btn_delete = itemView.findViewById(R.id.btn_delete);
        }
    }
}
