package br.com.codetec.saudesabor.ui.login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import br.com.codetec.saudesabor.R;
import br.com.codetec.saudesabor.model.Estado;
import br.com.codetec.saudesabor.model.ResponseCadastro;
import br.com.codetec.saudesabor.model.ResponseEstados;
import br.com.codetec.saudesabor.service.SaudeSaborService;
import br.com.codetec.saudesabor.service.ServiceConector;
import br.com.codetec.saudesabor.utils.components.alert.CustomAlert;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnderecoActivity extends AppCompatActivity {

    private List<Estado> listaEstados = new ArrayList<>();
    private SaudeSaborService saudeSaborService = ServiceConector.getService(EnderecoActivity.this);

    private EditText firstName;
    private EditText lastName;
    private EditText zipcode;
    private EditText address1;
    private EditText address2;
    private EditText email, telephone, password;
    private Spinner state;
    private EditText city;
    private Button btnCadastrarEndereco;
    private CheckBox enderecoPadrao;

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        firstName = findViewById(R.id.firstName);
        lastName = findViewById(R.id.lastName);
        lastName = findViewById(R.id.lastName);
        email = findViewById(R.id.email);
        telephone = findViewById(R.id.telephone);
        password = findViewById(R.id.password);
        zipcode = findViewById(R.id.zipcode);
        address1 = findViewById(R.id.address1);
        address2 = findViewById(R.id.address2);
        state = findViewById(R.id.spinnerZone);
        city = findViewById(R.id.city);
        btnCadastrarEndereco = findViewById(R.id.btnCadastrarEndereco);
        enderecoPadrao = findViewById(R.id.enderecoPadrao);

        carregarEstados(state);

        btnCadastrarEndereco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cadastrarEndereco();
            }
        });
    }

    public void cadastrarEndereco() {

        String zoneId = ((Estado) state.getSelectedItem()).getZone_id();

        Call<ResponseCadastro> call = saudeSaborService.cadastrarEndereco(
                LoginActivity.responseLogin.getUsuario().getId(),
                "pt",
                firstName.getText().toString(),
                lastName.getText().toString(),
                address1.getText().toString(),
                address2.getText().toString(),
                city.getText().toString(),
                zipcode.getText().toString(),
                zoneId,
                email.getText().toString(),
                telephone.getText().toString(),
                password.getText().toString(),
                enderecoPadrao.isChecked() ? "1" : "0"
        );
        call.enqueue(new Callback<ResponseCadastro>() {
            @Override
            public void onResponse(Call<ResponseCadastro> call, Response<ResponseCadastro> response) {
                if(response.body()!=null){
                    CustomAlert.getInstance().show(EnderecoActivity.this, response.body().getMensagem(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            EnderecoActivity.this.finish();
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<ResponseCadastro> call, Throwable t) {

            }
        });
    }

    private void carregarEstados(Spinner state) {
        //Carregar Estados
        Call<ResponseEstados> callEstados = saudeSaborService.consultarEstados(getString(R.string.locale));
        callEstados.enqueue(new Callback<ResponseEstados>() {
            @Override
            public void onResponse(Call<ResponseEstados> call, Response<ResponseEstados> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listaEstados = response.body().getEstados();
                    if (listaEstados == null) return;
                    refreshComboEstados(state);
                }
            }

            @Override
            public void onFailure(Call<ResponseEstados> call, Throwable t) {
                CustomAlert.getInstance().show(EnderecoActivity.this, getString(R.string.erro_conectividade));
            }
        });
    }

    private void refreshComboEstados(Spinner state) {
        state.setAdapter(new ArrayAdapter<Estado>(EnderecoActivity.this,R.layout.layout_spinner, listaEstados));
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        CustomAlert.getInstance().show(
                this, getString(R.string.app_name),
                getString(R.string.alerta_perdera_dados),
                getString(R.string.nao),
                getString(R.string.sim),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EnderecoActivity.this.finish();
                    }
                });

        return super.onOptionsItemSelected(item);
    }
}
