package it.integry.integrywmsnative.view.dialogs.printSsccUl;

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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogPrintUlSsccBinding;
import it.integry.integrywmsnative.view.dialogs.printSsccUl.dto.PrintUlDTO;

public class DialogPrintUlSSCCView extends BaseDialogFragment {


    private Context mContext;
    private DialogPrintUlSsccBinding mBindings;
    private final PrintUlDTO printUlDTO;
    private final RunnableArgs<PrintUlDTO> onDialogClose;


    public static DialogPrintUlSSCCView newInstance(
            String codJcom, MtbColt mtbColt,
            RunnableArgs<PrintUlDTO> onDismiss
    ) {
        return new DialogPrintUlSSCCView(codJcom, mtbColt, onDismiss);
    }

    public DialogPrintUlSSCCView(@NotNull String codJcom, @NotNull MtbColt mtbColt, @NotNull RunnableArgs<PrintUlDTO> onDismiss) {
        this.onDialogClose = onDismiss;

        this.printUlDTO = new PrintUlDTO()
                .setMtbColt(mtbColt)
                .setFlagPrintShipmentLabel(!UtilityString.isNullOrEmpty(codJcom))
                .setFlagPrintProductionLabel(UtilityString.isNullOrEmpty(codJcom));
    }


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

        mBindings = DialogPrintUlSsccBinding.inflate(inflater, container, false);

        MainApplication.appComponent
                .dialogPrintUlSSCCComponent()
                .create();

        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.printUlDTO.setFlagPrintProductionLabel(id == mBindings.printProductionLabel.getId());
        this.printUlDTO.setFlagPrintShipmentLabel(id == mBindings.printShippingLabel.getId());

    }

    public void onPositiveClick() {
        this.dismiss();
    }

    public void onNegativeClick() {
        this.printUlDTO.setFlagPrintProductionLabel(false);
        this.printUlDTO.setFlagPrintShipmentLabel(false);
        this.dismiss();
    }

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

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

    @Override
    public void onStart() {
        if (this.printUlDTO.isFlagPrintShipmentLabel()) {
            mBindings.printShippingLabel.setChecked(true);
        } else {
            mBindings.printProductionLabel.setChecked(true);
        }
        super.onStart();
    }


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