package com.younki.maidmatching.ui.auth;

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

import com.younki.maidmatching.services.SharedPreference.Preferences;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.services.HTTP.LoginRequest;
import com.younki.maidmatching.services.HTTP.LoginResponse;
import com.younki.maidmatching.ui.maid.MasterMaidActivity;
import com.younki.maidmatching.ui.owner.MasterOwnerActivity;
import com.younki.maidmatching.utils.BaseActivity;

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

public class LoginActivity extends BaseActivity {
    TextView txt_reg;
    Button btn_log;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

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

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

        btn_log = findViewById(R.id.btn_login);
        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

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

    public void login(){
        EditText email = findViewById(R.id.input_email_log);
        EditText password = findViewById(R.id.input_password_log);
        LoginRequest loginRequest = new LoginRequest();

        loginRequest.setEmail(getEditTextValue(email));
        loginRequest.setPassword(getEditTextValue(password));

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

        progressDialog.show(); // Menampilkan dialog


        Call<LoginResponse> loginResponseCall = ApiClient.getUserService(LoginActivity.this).userLogin(loginRequest);
        loginResponseCall.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                progressDialog.dismiss();

                if (response.isSuccessful()){
                    LoginResponse loginResponse = response.body();
                    if (!loginResponse.isError()) {
                        Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_SHORT).show();

                        Preferences.setToken(getBaseContext(), loginResponse.getData().getToken());
                        Preferences.setStatus(getBaseContext(), true);

                        Preferences.setEmail(getBaseContext(), getEditTextValue(email));
                        Preferences.setPassword(getBaseContext(), getEditTextValue(password));
                        Preferences.setUserId(getBaseContext(), loginResponse.getData().getUser().getId());
                        Preferences.setUsername(getBaseContext(), loginResponse.getData().getUser().getUsername());
                        Preferences.setRole(getBaseContext(), loginResponse.getData().getUser().getRole());

                        Preferences.setUserStatus(getBaseContext(), loginResponse.getData().getUser().getStatus());

                        Preferences.setUserSkills(getBaseContext(), loginResponse.getData().getUser().getSkill_count());

                        Preferences.setUserLocation(getBaseContext(), loginResponse.getData().getUser().getLocation());

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (loginResponse.getData().getUser().getRole() == 2) {
                                    Intent to_log = new Intent(LoginActivity.this, MasterOwnerActivity.class);
                                    startActivity(to_log);
                                    overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_top);
                                    finish();
                                } else {
                                    // Maid
                                    Intent to_log = new Intent(LoginActivity.this, MasterMaidActivity.class);
                                    startActivity(to_log);
                                    overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_top);
                                    finish();
                                }
                            }
                        }, 700);
                    } else {
                        Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginActivity.this, "Login Gagal.", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(LoginActivity.this, "Throwable "+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}