package it.integry.integrywmsnative.gest.prod_dettaglio_linea.dialogs;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import it.integry.integrywmsnative.databinding.BottomSheetLineActionsBinding;
import it.integry.integrywmsnative.gest.prod_linee_produzione.dto.ProdLineStatusDTO;

public class BottomSheetLineAction extends BottomSheetDialogFragment {

    private BottomSheetLineActionsBinding mBinding;

    private ProdLineStatusDTO prodLine;
    private Runnable onChangeResourcesRequest;
    private Runnable onChangeOrdersRequest;
    private Runnable onLineCloseRequest;
    private Runnable onRecoverRequest;
    private Runnable onCancelAction;

    public static BottomSheetLineAction newInstance(ProdLineStatusDTO dto, Runnable onChangeResourcesRequest, Runnable onChangeOrdersRequest, Runnable onLineCloseRequest, Runnable onRecoverRequest, Runnable onCancel) {
        BottomSheetLineAction fragment = new BottomSheetLineAction();
        fragment
                .setProdLine(dto)
                .setOnChangeResourcesRequest(onChangeResourcesRequest)
                .setOnChangeOrdersRequest(onChangeOrdersRequest)
                .setOnRecoverRequest(onRecoverRequest)
                .setOnLineCloseRequest(onLineCloseRequest)
                .setOnCancelAction(onCancel);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = BottomSheetLineActionsBinding.inflate(inflater, container, false);
        mBinding.setLifecycleOwner(this);
        mBinding.setView(this);
        mBinding.setListModel(this.prodLine);

        return mBinding.getRoot();
    }

    public BottomSheetLineAction setOnCancelAction(Runnable onCancelAction) {
        this.onCancelAction = onCancelAction;
        return this;
    }

    public BottomSheetLineAction setOnChangeResourcesRequest(Runnable onChangeResourcesRequest) {
        this.onChangeResourcesRequest = onChangeResourcesRequest;
        return this;
    }


    public BottomSheetLineAction setOnChangeOrdersRequest(Runnable onChangeOrdersRequest) {
        this.onChangeOrdersRequest = onChangeOrdersRequest;
        return this;
    }

    public BottomSheetLineAction setOnRecoverRequest(Runnable onRecoverRequest) {
        this.onRecoverRequest = onRecoverRequest;
        return this;
    }


    public BottomSheetLineAction setOnLineCloseRequest(Runnable onLineCloseRequest) {
        this.onLineCloseRequest = onLineCloseRequest;
        return this;
    }

    public ProdLineStatusDTO getProdLine() {
        return prodLine;
    }

    public BottomSheetLineAction setProdLine(ProdLineStatusDTO prodLine) {
        this.prodLine = prodLine;
        return this;
    }

    public void onResourcesClicked() {
        if (this.onChangeResourcesRequest != null) {
            this.onChangeResourcesRequest.run();
        }
        this.dismiss();

    }

    public void onPlayClicked() {
        if (this.onChangeOrdersRequest != null) {
            this.onChangeOrdersRequest.run();
        }
        this.dismiss();
    }

    public void onStopClicked() {
        if (this.onLineCloseRequest != null) {
            this.onLineCloseRequest.run();
        }
        this.dismiss();
    }

    public void onRecoverClicked() {
        if (this.onRecoverRequest != null) {
            this.onRecoverRequest.run();
        }
        this.dismiss();
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        if (this.onCancelAction != null) {
            onCancelAction.run();
        }
        super.onCancel(dialog);
    }
}
