package br.com.codetec.saudesabor.service;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.List;

import br.com.codetec.saudesabor.R;
import br.com.codetec.saudesabor.convert.ItensConvert;
import br.com.codetec.saudesabor.model.EnderecoUsuario;
import br.com.codetec.saudesabor.model.Formas;
import br.com.codetec.saudesabor.model.Formas_pagamento;
import br.com.codetec.saudesabor.model.Itens;
import br.com.codetec.saudesabor.model.ResponseEnderecos;
import br.com.codetec.saudesabor.model.ResponseFormasEnvio;
import br.com.codetec.saudesabor.model.ResponseFormasPagamento;
import br.com.codetec.saudesabor.ui.login.LoginActivity;
import br.com.codetec.saudesabor.ui.login.ui.notifications.CarrinhoFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarrinhoService {


    private Context context;

    public CarrinhoService(Context context){
        this.context = context;
    }

    public void carregarListaEnderecos(Spinner spinnerListaEnderecos){
        Call<ResponseEnderecos> call =  ServiceConector
                                            .getService(this.context)
                                            .listarEnderecosUsuario(LoginActivity.responseLogin.getToken());
        call.enqueue(new Callback<ResponseEnderecos>() {
            @Override
            public void onResponse(Call<ResponseEnderecos> call, Response<ResponseEnderecos> response) {
                if(response.body()!=null){
                    spinnerListaEnderecos.setAdapter(
                            new ArrayAdapter<EnderecoUsuario>(
                                    context,
                                    R.layout.layout_spinner,
                                    response.body().getEnderecos()
                            )
                    );
                }
            }

            @Override
            public void onFailure(Call<ResponseEnderecos> call, Throwable t) {
            }
        });
    }

    public void carregarFormasDeEnvio(Spinner spinnerFormaDeEnvio, Spinner spinnerData, Spinner spinnerHorario ){
        Call<ResponseFormasEnvio> call =  ServiceConector.getService(context).listaFormasEnvio(context.getString(R.string.locale));
        call.enqueue(new Callback<ResponseFormasEnvio>() {
            @Override
            public void onResponse(Call<ResponseFormasEnvio> call, Response<ResponseFormasEnvio> response) {
                if(response.body()!=null) {
                    ResponseFormasEnvio responseFormasEnvio = response.body();

                    if (responseFormasEnvio.getFormas() != null && responseFormasEnvio.getFormas().size() > 0) {
                        spinnerFormaDeEnvio.setAdapter(
                                new ArrayAdapter<Formas>(
                                        context,
                                        R.layout.layout_spinner,
                                        response.body().getFormas()
                                )
                        );

                        spinnerFormaDeEnvio.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                spinnerData.setAdapter(
                                        new ArrayAdapter<String>(
                                                context,
                                                R.layout.layout_spinner,
                                                responseFormasEnvio.getFormas().get(position).getDatas_list()
                                        )
                                );

                                if (responseFormasEnvio.getFormas().get(position).getHorarios_list() != null
                                        && responseFormasEnvio.getFormas().get(position).getHorarios_list().size() > 0) {
                                    spinnerHorario.setAdapter(
                                            new ArrayAdapter<String>(
                                                    context,
                                                    R.layout.layout_spinner,
                                                    responseFormasEnvio.getFormas().get(position).getHorarios_list()
                                            )
                                    );
                                }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {
                            }
                        });
                    }
                }
            }
            @Override
            public void onFailure(Call<ResponseFormasEnvio> call, Throwable t) {
            }
        });
    }

    public void carregarFormasDePagamento(Spinner spinnerFormaPagamento, CarrinhoFragment fragment){
        Call<ResponseFormasPagamento> call =  ServiceConector.getService(context).listaFormasPagamento(context.getString(R.string.locale));
        call.enqueue(new Callback<ResponseFormasPagamento>() {
            @Override
            public void onResponse(Call<ResponseFormasPagamento> call, Response<ResponseFormasPagamento> response) {
                if(response.body()!=null){
                    spinnerFormaPagamento.setAdapter(
                            new ArrayAdapter<Formas_pagamento>(
                                    context,
                                    R.layout.layout_spinner,
                                    response.body().getFormas_pagamento()
                            )
                    );

                    fragment.getCalculoTotalComPontos();
                }
            }

            @Override
            public void onFailure(Call<ResponseFormasPagamento> call, Throwable t) {
            }
        });
    }

    public List<Itens> listaItens(){
        return new ItensConvert().convertListProdutoToItens(CarrinhoComprasGlobal.listaProdutos());
    }
}
