package it.integry.integrywmsnative.gest.prod_rientro_merce;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.databinding.DataBindingUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseFragment;
import it.integry.integrywmsnative.core.interfaces.IScrollableFragment;
import it.integry.integrywmsnative.core.interfaces.ITitledFragment;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.FragmentProdRientroMerceBinding;
import it.integry.integrywmsnative.gest.prod_rientro_merce.order_detail.ProdRientroMerceOrderDetailFragment;
import it.integry.integrywmsnative.gest.prod_rientro_merce.order_list.ProdRientroMerceOrderListFragment;
import it.integry.integrywmsnative.gest.prod_versamento_materiale.dto.OrdineLavorazioneDTO;
import it.integry.integrywmsnative.ui.ElevatedToolbar;
import it.integry.integrywmsnative.view.bottom_sheet__lu_content.BottomSheetFragmentLUContentView;
import it.integry.integrywmsnative.view.bottom_sheet__mtb_colr_edit.BottomSheetMtbColrEditView;
import it.integry.integrywmsnative.view.dialogs.base.DialogSimpleMessageView;

public class ProdRientroMerceFragment extends BaseFragment implements ITitledFragment, IScrollableFragment, ProdRientroMerceViewModel.Listener, BottomSheetFragmentLUContentView.Listener, BottomSheetMtbColrEditView.Listener {


    @Inject
    ProdRientroMerceViewModel mViewModel;

    private AppCompatTextView mAppBarTitle;
    private final List<Runnable> mOnPreDestroyList = new ArrayList<>();

    private FragmentProdRientroMerceBinding mBindings;

    private ProdRientroMerceOrderListFragment mProdRientroMerceOrderListFragment;
    private ProdRientroMerceOrderDetailFragment mProdRientroMerceOrderDetailFragment;
    private ElevatedToolbar mToolbar;

    public static ProdRientroMerceFragment newInstance() {
        return new ProdRientroMerceFragment();
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        mProdRientroMerceOrderListFragment = ProdRientroMerceOrderListFragment.newInstance(mToolbar);
        mProdRientroMerceOrderDetailFragment = ProdRientroMerceOrderDetailFragment.newInstance();

        mProdRientroMerceOrderListFragment.setOnItemSelectedRunnable(mProdRientroMerceOrderDetailFragment::setOrder);
        mProdRientroMerceOrderDetailFragment.setOnMtbColtClicked(item -> {
            mBindings.bottomSheetMtbColrEdit.setMtbColr(item.getMtbColr().get(0));
            mBindings.bottomSheetMtbColrEdit.expand();
        });

        getChildFragmentManager()
                .beginTransaction()
                .replace(R.id.order_list_fragment, mProdRientroMerceOrderListFragment)
                .replace(R.id.order_detail_fragment, mProdRientroMerceOrderDetailFragment)
                .commit();

        MainApplication.appComponent
                .prodRientroMerceComponent()
                .create()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBindings = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_prod_rientro_merce, container, false);
        mBindings.setLifecycleOwner(this);

        mViewModel.setListener(this);

        this.initBottomSheet();

        return mBindings.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if(savedInstanceState == null) {
            this.mViewModel.getOrdiniLavorazioneAperti().observe(this.getViewLifecycleOwner(), this::ordersUpdated);

            mViewModel.init();
        }
    }

    @Override
    public void onCreateActionBar(AppCompatTextView titleText, Context context) {
        mAppBarTitle = titleText;
        mAppBarTitle.setText(context.getText(R.string.rientro_merce_fragment_title).toString());

    }

    @Override
    public void addOnPreDestroy(Runnable onPreDestroy) {
        this.mOnPreDestroyList.add(onPreDestroy);
    }

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

        for (Runnable onPreDestroy :
                mOnPreDestroyList) {
            onPreDestroy.run();
        }
    }

    @Override
    public void onDataSaved() {
    }

    private void ordersUpdated(List<OrdineLavorazioneDTO> ordiniLavorazioneList) {
        this.mProdRientroMerceOrderListFragment.pushOrderList(ordiniLavorazioneList);
    }

    @Override
    public void setScrollToolbar(ElevatedToolbar toolbar) {
        mToolbar = toolbar;
    }



    private void initBottomSheet() {
        mBindings.bottomSheetMtbColrEdit.setListener(this);
        mBindings.bottomSheetMtbColrEdit.disableEditButton();
        mBindings.bottomSheetMtbColrEdit.init(mBindings.bottomSheetMtbColrEdit, mBindings.bottomSheetMtbColrEditBackground);
    }

    @Override
    public void onMtbColrEdit(MtbColr mtbColr) {

    }

    @Override
    public void onMtbColrDelete(MtbColr mtbColr) {
        String text = getResources().getString(R.string.alert_delete_mtb_colr);
        DialogSimpleMessageView.makeWarningDialog(new SpannableString(text),
                null,
                () -> mProdRientroMerceOrderDetailFragment.deleteMtbColr(mtbColr),
                () -> {}
        ).show(getActivity().getSupportFragmentManager(), "tag");
    }
}