package it.integry.integrywmsnative.view.bottom_sheet__lu_content;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.OnListGeneralChangedCallback;
import it.integry.integrywmsnative.core.expansion.OnSingleClickListener;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.databinding.BottomSheetFragmentLuContentBinding;
import it.integry.integrywmsnative.view.bottom_sheet__base.BottomSheetFragmentBaseView;
import it.integry.integrywmsnative.view.bottom_sheet__mtb_colr_edit.BottomSheetMtbColrEditModalView;

public class BottomSheetFragmentLUContentView extends BottomSheetFragmentBaseView implements BottomSheetMtbColrEditModalView.Listener {

    private BottomSheetFragmentLuContentBinding mBinding;
    private BottomSheetFragmentLUContentViewModel mViewModel;

    private BottomSheetFragmentLUContentListAdapter mAdapter;

    private Listener mListener;

    public BottomSheetFragmentLUContentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.bottom_sheet_fragment__lu_content, this, true);


        mViewModel = new BottomSheetFragmentLUContentViewModel();
        mBinding.setViewModel(mViewModel);


        mBinding.titleLayout.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                toggle();
            }
        });

        this.initCallbacks();
    }

    public BottomSheetFragmentLUContentViewModel getViewModelInstance() {
        return mViewModel;
    }


    private void initAdapter() {

        this.mBinding.mtbColrRecyclerView.setHasFixedSize(true);
        this.mBinding.mtbColrRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        this.mAdapter = new BottomSheetFragmentLUContentListAdapter(this.mContext, this.mViewModel.getObservableMtbColt().get().getMtbColr());
        this.mBinding.mtbColrRecyclerView.setAdapter(this.mAdapter);

        this.mAdapter.setOnItemClickListener(clickedMtbColr -> {

            new BottomSheetMtbColrEditModalView(clickedMtbColr)
                    .setListener(this)
                    .show(((AppCompatActivity) mContext).getSupportFragmentManager(), "tag");

        });
    }

    private void initCallbacks() {
        this.mViewModel.getObservableMtbColt().addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                var mtbColt = mViewModel.getObservableMtbColt().get();

                if (mtbColt != null) {
                    initAdapter();

                    onMtbColrItemChanged();

                    mtbColt.getMtbColr().addOnListChangedCallback(new OnListGeneralChangedCallback() {
                        @Override
                        public void onChanged(ObservableList sender) {
                            onMtbColrItemChanged();
                        }
                    });
                } else {
                    mBinding.mtbColrRecyclerView.setAdapter(null);
                }
            }
        });
    }

    private void onMtbColrItemChanged() {
        List<MtbColr> mtbColrs = mViewModel.getObservableMtbColt().get().getMtbColr();
        mBinding.textviewArtCounter.setText(String.valueOf(mtbColrs.size()));
        mBinding.textviewArtDescription.setText(mContext.getResources().getQuantityString(R.plurals.articles, mtbColrs.size()));
        mViewModel.calcPesi();
    }

    public BottomSheetFragmentLUContentView setListener(Listener listener) {
        this.mListener = listener;
        return this;
    }

    @Override
    protected void onStateChangedCallback(State newState) {
        switch (newState) {
            case EXPANDED:
                this.mBinding.btnCloseLu.setClickable(false);
                this.mBinding.btnCloseLu.setFocusable(false);
                break;

            case COLLAPSED:
                this.mBinding.btnCloseLu.setClickable(true);
                this.mBinding.btnCloseLu.setFocusable(true);
                break;
        }
    }

    @Override
    protected void onSlideCallback(float slideOffset) {
        this.mBinding.btnCloseLu.setAlpha(slideOffset);
//        mBinding.tapActionLayout.setAlpha(1-slideOffset);
    }

    @Override
    public void onMtbColrEdit(MtbColr mtbColr) {
        if (mListener != null) this.mListener.onMtbColrEdit(mtbColr);
    }

    @Override
    public void onMtbColrDelete(MtbColr mtbColr) {
        if (mListener != null) this.mListener.onMtbColrDelete(mtbColr);
    }


    public interface Listener {
        void onMtbColrEdit(MtbColr mtbColr);

        void onMtbColrDelete(MtbColr mtbColr);
    }

}
