package com.younki.maidmatching.ui;

import static android.content.ContentValues.TAG;

import static androidx.core.content.ContextCompat.startActivity;
import android.Manifest;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.squareup.picasso.Picasso;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.UserResponse;
import com.younki.maidmatching.utils.CircleTransform;

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

public class OwnerDialog extends Dialog {
    private static OwnerDialog activeDialog;
    private Context context;
    private int userId;
    private static final int REQUEST_CALL_PHONE = 1;

    public OwnerDialog(@NonNull Context context, int userId) {
        super(context);
        this.context = context;
        this.userId = userId;
    }

    public static OwnerDialog 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_owner_dialog);

        getOwnerData();

    }

    private void getOwnerData() {
        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

        TextView name = findViewById(R.id.name);
        TextView username = findViewById(R.id.username);
        TextView email = findViewById(R.id.email);
        TextView phone = findViewById(R.id.phone);
        TextView gender = findViewById(R.id.gender);
        TextView last_education = findViewById(R.id.last_education);

        Call<UserResponse> userResponseCall = ApiClient.getUserService(context).getUserDetail(String.valueOf(userId));

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

                if (response.isSuccessful()){
                    UserResponse userResponse = response.body();

                    name.setText(userResponse.getData().get(0).getName());
                    email.setText(userResponse.getData().get(0).getEmail());
                    phone.setText(userResponse.getData().get(0).getProfile().getPhone());
                    gender.setText(userResponse.getData().get(0).getProfile().getGender());
                    last_education.setText(userResponse.getData().get(0).getProfile().getLast_education());
                    username.setText("@"+userResponse.getData().get(0).getUsername());
                    Picasso.get().load(ApiClient.getBaseUrl()+userResponse.getData().get(0).getProfile().getImage())
                            .into((ImageView) findViewById(R.id.profile));

                    Button btn_hubungi = findViewById(R.id.btn_hubungi);
                    btn_hubungi.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            makePhoneCall(userResponse.getData().get(0).getProfile().getPhone());
                        }
                    });
                } else {
                    Toast.makeText(context, "Gagal Mengambil Data", Toast.LENGTH_SHORT).show();
                }
            }

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

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

    private void makePhoneCall(String phoneNumber) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
//            makePhoneCall(phoneNumber);
        } else {
            try {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + phoneNumber));
                context.startActivity(callIntent);
            } catch (Exception e) {
                Toast.makeText(context, "Nomor telepon tidak ditemukan!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
