package it.integry.integrywmsnative.gest.accettazione;


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

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.annimon.stream.Stream;

import java.math.BigDecimal;
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.data_cache.DataCache;
import it.integry.integrywmsnative.core.di.BindableBoolean;
import it.integry.integrywmsnative.core.expansion.BaseFragment;
import it.integry.integrywmsnative.core.interfaces.IScrollableFragment;
import it.integry.integrywmsnative.core.interfaces.ISearchableFragment;
import it.integry.integrywmsnative.core.interfaces.ITitledFragment;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.UtilityBigDecimal;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.databinding.FragmentMainAccettazioneBinding;
import it.integry.integrywmsnative.gest.accettazione.ui.MainListAccettazioneAdapter;
import it.integry.integrywmsnative.gest.accettazione.ui.MainListAccettazioneClienteListModel;
import it.integry.integrywmsnative.gest.accettazione.ui.MainListAccettazioneListModel;
import it.integry.integrywmsnative.gest.accettazione.dto.OrdineAccettazioneInevasoDTO;
import it.integry.integrywmsnative.gest.accettazione.dto.SitArtOrdDTO;
import it.integry.integrywmsnative.gest.accettazione_picking.AccettazionePickingActivity;
import it.integry.integrywmsnative.ui.ElevatedToolbar;
import it.integry.integrywmsnative.view.dialogs.base.DialogSimpleMessageView;

public class MainAccettazioneFragment extends BaseFragment implements ISearchableFragment, ITitledFragment, IScrollableFragment, MainAccettazioneViewModel.Listener {

    public BindableBoolean fabVisible = new BindableBoolean(false);

    @Inject
    MainAccettazioneViewModel mViewModel;

    private final List<Runnable> mOnPreDestroyList = new ArrayList<>();
    private ElevatedToolbar mToolbar;

    private String mTextFilter;
    private FragmentMainAccettazioneBinding mBinding;

    private final ObservableArrayList<MainListAccettazioneListModel> mOrdiniInevasiMutableData = new ObservableArrayList<>();

    private AppCompatTextView mAppBarTitle;


    public MainAccettazioneFragment() {
        // Required empty public constructor
    }

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


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

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        onLoadingEnded();
        outState.putString("mToolbar", DataCache.addItem(mToolbar));

        super.onSaveInstanceState(outState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(savedInstanceState != null && savedInstanceState.containsKey("mToolbar"))
            mToolbar = DataCache.retrieveItem(savedInstanceState.getString("mToolbar"));

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main_accettazione, container, false);

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

        mViewModel.setListener(this);

        mBinding.setLifecycleOwner(this);
        mBinding.setView(this);

        this.initRecyclerView();

        mToolbar.setRecyclerView(mBinding.accettazioneMainList);

        return mBinding.getRoot();
    }


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

        this.fabVisible.set(false);

        String codMdep = SettingsManager.i().getUserSession().getDepo().getCodMdep();
        mViewModel.init(codMdep);
    }

    @Override
    public void onDestroy() {
        for (Runnable onPreDestroy : mOnPreDestroyList) {
            onPreDestroy.run();
        }
        super.onDestroy();
    }


    private void initRecyclerView() {
        this.mViewModel.getOrderList().observe(getViewLifecycleOwner(), x->{
            this.refreshList(getFilteredList(mTextFilter));
        });


        MainListAccettazioneAdapter adapter = new MainListAccettazioneAdapter(getActivity(), mOrdiniInevasiMutableData)
                .setOnGroupItemClicked(x -> {
                    Stream.of(mOrdiniInevasiMutableData)
                            .filter(y -> !y.getGroupTitle().equalsIgnoreCase(x) && y.getSelectedObservable().get())
                            .forEach(y -> y.getSelectedObservable().set(false));

                    boolean allSelected = Stream.of(mOrdiniInevasiMutableData)
                            .filter(y -> y.getGroupTitle().equalsIgnoreCase(x))
                            .allMatch(y -> y.getSelectedObservable().get());

                    Stream.of(mOrdiniInevasiMutableData)
                            .filter(y -> y.getGroupTitle().equalsIgnoreCase(x))
                            .forEach(y -> y.getSelectedObservable().set(!allSelected));
                })
                .setOnItemChecked(x -> {
                    Stream.of(mOrdiniInevasiMutableData)
                            .filter(y -> !y.getGroupTitle().equalsIgnoreCase(x.getGroupTitle()) && y.getSelectedObservable().get())
                            .forEach(y -> y.getSelectedObservable().set(false));

                    fabVisible.set(Stream.of(mOrdiniInevasiMutableData)
                            .anyMatch(y -> y.getSelectedObservable().get()));
                });

        adapter.setEmptyView(this.mBinding.ordiniAccettazioneEmptyView);

        this.mBinding.accettazioneMainList.setAdapter(adapter);
        this.mBinding.accettazioneMainList.setLayoutManager(new LinearLayoutManager(getActivity()));

        if (mToolbar != null)
            mToolbar.setRecyclerView(this.mBinding.accettazioneMainList);
    }


    private void refreshList(List<OrdineAccettazioneInevasoDTO> filteredList) {

        List<OrdineAccettazioneInevasoDTO> tmpList = null;

        if (filteredList != null) {
            tmpList = filteredList;
        } else {
            tmpList = mViewModel.getOrderList().getValue();
        }

        this.mOrdiniInevasiMutableData.clear();
        this.mOrdiniInevasiMutableData.addAll(convertDataModelToListModel(tmpList));
    }

    private List<MainListAccettazioneListModel> convertDataModelToListModel(List<OrdineAccettazioneInevasoDTO> dataList) {

        return Stream.of(dataList)
                .distinctBy(OrdineAccettazioneInevasoDTO::getBarcode)
                .sortBy(x -> x.getRagSocOrd() + (x.getDataConsD() != null ? UtilityDate.formatDate(x.getDataConsD(), UtilityDate.COMMONS_DATE_FORMATS.YMD_SLASH) : ""))
                .map(x -> {
                    MainListAccettazioneListModel listModel = new MainListAccettazioneListModel();

                    listModel.setOriginalModel(x);

                    listModel.setGroupTitle(x.getRagSocOrd());

                    listModel.setDescription(String.format(getString(R.string.ord_acq_testata), x.getNumero().toString(), UtilityDate.formatDate(x.getDataD(), UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN)));

                    List<MainListAccettazioneClienteListModel> clienti = Stream.of(dataList)
                            .filter(y -> y.getBarcode().equals(x.getBarcode()))
                            .map(y -> new MainListAccettazioneClienteListModel()
                                    .setPrimaryText(y.getRifOrd())
                                    .setRightText(UtilityDate.formatDate(y.getDataConsD(), UtilityDate.COMMONS_DATE_FORMATS.DMY_SLASH)))
                            .toList();

                    listModel.setClientiListModel(clienti);

                    return listModel;
                })
                .toList();
    }

    public void dispatchOrders() {
        List<OrdineAccettazioneInevasoDTO> selectedOrders = Stream.of(this.mOrdiniInevasiMutableData)
                .filter(x -> x.getSelectedObservable().get())
                .map(MainListAccettazioneListModel::getOriginalModel)
                .toList();


        this.mViewModel.loadPicking(selectedOrders);
    }


    @Override
    public void onSearchEnabled() {
        mAppBarTitle.setVisibility(View.GONE);
    }

    @Override
    public void onSearchDisabled() {
        mAppBarTitle.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        List<OrdineAccettazioneInevasoDTO> originalList = this.mViewModel.getOrderList().getValue();
        if(originalList == null || originalList.isEmpty()) return false;
        mTextFilter = newText;
        List<OrdineAccettazioneInevasoDTO> filteredOrders = getFilteredList(newText);

        refreshList(filteredOrders);
        return true;
    }

    private List<OrdineAccettazioneInevasoDTO> getFilteredList(String newText) {
        List<OrdineAccettazioneInevasoDTO> filteredOrders = new ArrayList<>();
        List<OrdineAccettazioneInevasoDTO> originalList = this.mViewModel.getOrderList().getValue();
        if(originalList == null || originalList.isEmpty() || newText == null) return originalList;
        for(int i = 0; i < originalList.size(); i++){
            if(originalList.get(i).getRagSocOrd().toLowerCase().contains(newText.toLowerCase())){
                filteredOrders.add(originalList.get(i));
            }
        }
        return filteredOrders;
    }

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


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

    @Override
    public void onOrdersDispatched(List<OrdineAccettazioneInevasoDTO> orders, List<SitArtOrdDTO> sitArts) {
        long artsCounter = Stream.of(sitArts)
                .filter(x -> UtilityBigDecimal.greaterThan(x.getQtaDaEvadere(), BigDecimal.ZERO))
                .map(SitArtOrdDTO::getCodMart)
                .distinct()
                .count();

        long ordsCounter = Stream.of(sitArts)
                .distinctBy(x -> x.getDataOrd() + " " + x.getNumOrd() + " " + x.getGestione())
                .count();


        DialogSimpleMessageView.makeInfoDialog(getText(R.string.orders).toString(),
                Html.fromHtml(String.format(getActivity().getResources().getQuantityString(R.plurals.loaded_orders_message, (int) ordsCounter), ordsCounter)
                        + "<br /><br />" +
                        "<b>" + artsCounter + "</b> " + getActivity().getResources().getQuantityString(R.plurals.available_articles, (int) artsCounter)),
                null,
                () -> {

                    String cacheSitArtItemID = DataCache.addItem(sitArts);
                    String cacheOrdersItemID = DataCache.addItem(orders);

                    Intent myIntent = new Intent(getActivity(), AccettazionePickingActivity.class);
                    myIntent.putExtra("keyOrders", cacheOrdersItemID);
                    myIntent.putExtra("keySitArts", cacheSitArtItemID);
                    getActivity().startActivity(myIntent);

                }).show(getActivity().getSupportFragmentManager(), "tag");
    }
}
