package com.younki.maidmatching.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.squareup.picasso.Picasso;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.Adapter.JobData;
import com.younki.maidmatching.services.ApiClient;

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

public class ShowDetailJobDialog extends Dialog {
    private static ShowDetailJobDialog activeDialog;
    private Context context;
    private JobData jobData;

    public ShowDetailJobDialog(@NonNull Context context, JobData jobData) {
        super(context);
        this.context = context;
        this.jobData = jobData;
    }

    public static ShowDetailJobDialog getActiveDialog() {
        return activeDialog;
    }

    @Override
    protected void onStart() {
        super.onStart();

        // set activeDialog ke instance dari dialog yang sedang aktif
        activeDialog = this;
    }

    @Override
    protected void onStop() {
        super.onStop();

        // set activeDialog ke null ketika dialog tidak lagi aktif
        activeDialog = null;
    }

    public void dissmissDialog() {
        // cek apakah dialog sedang aktif
        if (activeDialog != null) {
            // dialog sedang aktif, lakukan sesuatu di sini
            dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_detail_job_dialog);
        NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("id", "ID"));
        TextView salary, name, applicant, recommended_age, description, address, skills, accommodation, working_days, working_hours;
        ImageView job_image;

        salary = findViewById(R.id.salary);
        name = findViewById(R.id.name);
        applicant = findViewById(R.id.applicant);
        recommended_age = findViewById(R.id.recommended_age);
        job_image = findViewById(R.id.job_image);
        description = findViewById(R.id.description);
        address = findViewById(R.id.address);
        skills = findViewById(R.id.skills);
        accommodation = findViewById(R.id.accommodation);
        working_days = findViewById(R.id.working_days);
        working_hours = findViewById(R.id.working_hours);

        accommodation.setText(jobData.getAccommodation());
        working_days.setText(jobData.getWorking_days());
        working_hours.setText("Hari Kerja: ("+jobData.getWorking_hours()+" Jam/Hari)");
        salary.setText(formatter.format(Integer.valueOf(jobData.getSalary())));
        name.setText(jobData.getName());
        applicant.setText("Apply: "+jobData.getApplicant());
        recommended_age.setText("Rentang Umur: "+jobData.getRecommended_age());
        description.setText(jobData.getDescription());
        address.setText(jobData.getAddress());
        Picasso.get().load(ApiClient.getBaseUrl()+jobData.getImage()).into(job_image);

        String html = "<html><ul>";
        for (int i = 0; i < jobData.getSkills().size(); i++) {
            html += "<li>"+jobData.getSkills().get(i).getName()+"</li>";
        }
        html += "</ul></html>";
        skills.setText(Html.fromHtml(html));
    }
}
