package it.integry.integrywmsnative.gest.spedizione.dialogs.row_info.prod_fabbisogno_linee_prod;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.rest.model.SitArtOrdDTO;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.databinding.DialogRowInfoProdFabbisognoLineeProdBinding;
import it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.dto.ProdFabbisognoLineeProdDTO;
import it.integry.integrywmsnative.gest.spedizione.ui.SpedizioneListModel;
import it.integry.integrywmsnative.gest.spedizione.dialogs.row_info.BaseDialogRowInfoView;

public class DialogRowInfoProdFabbisognoLineeProdView extends BaseDialogRowInfoView implements DialogRowInfoProdFabbisognoLineeProdViewModel.Listener {


    @Inject
    DialogRowInfoProdFabbisognoLineeProdViewModel mViewModel;

    private Context mContext;
    private DialogRowInfoProdFabbisognoLineeProdBinding mBindings;

    public DialogRowInfoProdFabbisognoLineeProdView(@NotNull SpedizioneListModel spedizioneListModel, @NotNull Runnable onComplete, @NotNull Runnable onAbort) {
        super(spedizioneListModel, onComplete, onAbort);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mContext = getActivity();

        mBindings = DataBindingUtil.inflate(inflater, R.layout.dialog_row_info_prod_fabbisogno_linee_prod, container, false);

        MainApplication.appComponent
                .dialogRowInfoProdFabbisognoLineeProdComponent()
                .create()
                .inject(this);

        setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        this.mViewModel.setListener(this);

        return mBindings.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();

        SitArtOrdDTO sitArtOrdDTO = getSpedizioneListModel().getOriginalModel().getSitArtOrdDTO();

        this.mViewModel.getFabbisognoList().removeObservers(this);
        this.mViewModel.getFabbisognoList().observe(this, this::onFabbisognoListLoaded);

        this.mViewModel.init(
                sitArtOrdDTO.getCodMart(),
                sitArtOrdDTO.getCodMdep(),
                (Date) sitArtOrdDTO.getExtraInfo().get("dataInizio"),
                (Date) sitArtOrdDTO.getExtraInfo().get("dataFine"));
    }

    private void onFabbisognoListLoaded(List<ProdFabbisognoLineeProdDTO> fabbisognoList) {
        for (int i = 0; i < fabbisognoList.size(); i++) {
            String currentKey = fabbisognoList.get(i).getCodJfas();

            ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView tv = new TextView(this.mContext);
            tv.setLayoutParams(lparams);
            tv.setText(currentKey);

            this.mBindings.contentView.addView(tv);
        }
    }

    @Override
    public void onError(Exception ex) {
        UtilityExceptions.defaultException(this.mContext, ex);
        dismiss();
    }

    @Override
    public void onLoadingStarted() {
        this.mBindings.loadingView.setVisibility(View.VISIBLE);
        this.mBindings.contentView.setVisibility(View.GONE);
    }

    @Override
    public void onLoadingEnded() {
        this.mBindings.loadingView.setVisibility(View.GONE);
        this.mBindings.contentView.setVisibility(View.VISIBLE);
    }
}
