package it.integry.integrywmsnative.gest.spedizione.dialogs.print_sscc_list;

import android.content.Context;
import android.content.DialogInterface;
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.RadioGroup;

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

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

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.databinding.DialogPrintOrderSsccListBinding;
import it.integry.integrywmsnative.gest.spedizione.model.PrintOrderCloseDTO;

public class DialogPrintOrderSSCCListView extends BaseDialogFragment {


    @Inject
    DialogPrintOrderSSCCListViewModel mViewModel;

    private Context mContext;
    private DialogPrintOrderSsccListBinding mBindings;
    private final PrintOrderCloseDTO printOrderCloseDTO;
    private final RunnableArgs<PrintOrderCloseDTO> onDialogClose;


    public static DialogPrintOrderSSCCListView newInstance(
            PrintOrderCloseDTO printOrderCloseDTO,
            RunnableArgs<PrintOrderCloseDTO> onDismiss
    ) {
        return new DialogPrintOrderSSCCListView(printOrderCloseDTO, onDismiss);
    }

    public DialogPrintOrderSSCCListView(@NotNull PrintOrderCloseDTO printOrderCloseDTO, @NotNull RunnableArgs<PrintOrderCloseDTO> onDismiss) {
        this.printOrderCloseDTO = printOrderCloseDTO;
        this.onDialogClose = onDismiss;
    }


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

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

        MainApplication.appComponent
                .dialogPrintOrderSSCCListComponent()
                .create()
                .inject(this);
        mBindings.setView(this);
        setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        return mBindings.getRoot();
    }

    public void onCheckChange(RadioGroup radioGroup, int id) {
        this.printOrderCloseDTO.setFlagSkipPrintedSSCC(id == mBindings.printOnlyNew.getId());
    }

    public void onPositiveClick() {
        this.printOrderCloseDTO.setFlagPrintSSCC(true);
        this.dismiss();
    }

    public void onNegativeClick() {
        this.printOrderCloseDTO.setFlagPrintSSCC(false);
        this.dismiss();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        this.onDialogClose.run(this.printOrderCloseDTO);
        super.onDismiss(dialog);
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        this.onNegativeClick();
        super.onCancel(dialog);
    }

    @Override
    public void onStart() {
        if (this.printOrderCloseDTO.isFlagSkipPrintedSSCC()) {
            mBindings.printOnlyNew.setChecked(true);
        } else {
            mBindings.printAll.setChecked(true);
        }
        super.onStart();
    }


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