package com.younki.maidmatching.ui.auth;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.GlobalResponse;
import com.younki.maidmatching.services.HTTP.RegisterRequest;
import com.younki.maidmatching.utils.BaseActivity;

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

public class RegisterAsMaidActivity extends BaseActivity {
    CheckBox input_check_term, input_check_age;
    TextView txt_login, txt_owner_reg;
    Button btn_register_maid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_as_maid);

        btn_register_maid = findViewById(R.id.btn_register_maid);

        // Check Internet Status
        checkInternetStatus(RegisterAsMaidActivity.this);
        checkPreferencesAuth(false, RegisterAsMaidActivity.this);

        String text = "Dengan melanjutkan, Anda menyetujui <u>Kebijakan Privasi</u> dan <u>Syarat Penggunaan</u> kami.";
        input_check_term = findViewById(R.id.input_check_term);
        input_check_term.setText(Html.fromHtml(text));

        input_check_age = findViewById(R.id.input_check_age);
        input_check_age.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    btn_register_maid.setEnabled(true);
                } else {
                    btn_register_maid.setEnabled(false);
                }
            }
        });

        txt_login = findViewById(R.id.txt_login);
        txt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent to_login = new Intent(RegisterAsMaidActivity.this, LoginActivity.class);
                startActivity(to_login);
                overridePendingTransition(0, R.anim.slide_fade_out);
                finish();
            }
        });

        txt_owner_reg = findViewById(R.id.txt_owner_reg);
        txt_owner_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent to_owner_reg = new Intent(RegisterAsMaidActivity.this, RegisterAsOwnerActivity.class);
                startActivity(to_owner_reg);
                overridePendingTransition(0, R.anim.slide_fade_out);
                finish();
            }
        });

        btn_register_maid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkPreferencesAuth(true, RegisterAsMaidActivity.this);
    }

    private void register() {
        EditText input_first_name_reg, input_last_name_reg, input_email_reg, input_password_reg, input_password_confirm_reg;
        input_first_name_reg = findViewById(R.id.input_first_name_reg);
        input_last_name_reg = findViewById(R.id.input_last_name_reg);
        input_email_reg = findViewById(R.id.input_email_reg);
        input_password_reg = findViewById(R.id.input_password_reg);
        input_password_confirm_reg = findViewById(R.id.input_password_confirm_reg);

        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setRole(3);
        registerRequest.setName(getEditTextValue(input_first_name_reg)+" "+getEditTextValue(input_last_name_reg));
        registerRequest.setEmail(getEditTextValue(input_email_reg));
        registerRequest.setPassword(getEditTextValue(input_password_reg));
        registerRequest.setPassword_confirmation(getEditTextValue(input_password_confirm_reg));

        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading..."); // Set message untuk dialog
        progressDialog.setCancelable(false); // Set apakah dialog bisa di-cancel atau tidak
        progressDialog.show();

        Call<GlobalResponse> globalResponseCall = ApiClient.getUserService(this).userRegister(registerRequest);
        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(RegisterAsMaidActivity.this, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();

                        Intent to_reg = new Intent(RegisterAsMaidActivity.this, LoginActivity.class);
                        startActivity(to_reg);
                        overridePendingTransition(0, R.anim.slide_fade_out);
                        finish();
                    } else {
                        Toast.makeText(RegisterAsMaidActivity.this, globalResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RegisterAsMaidActivity.this, "Register Gagal.", Toast.LENGTH_SHORT).show();
                }
            }

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