package it.integry.integrywmsnative.view.bottomsheet;

import androidx.annotation.NonNull;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.databinding.FragmentArticoliInColloBottomSheetBinding;


public class ArticoliInColloBottomSheetHelper extends BottomSheetBehavior.BottomSheetCallback {

    private AppCompatActivity mActivity;
    private FragmentArticoliInColloBottomSheetBinding mBinding;


    @BindView(R.id.appbar_bottom_sheet) AppBarLayout appBarLayoutBottomSheet;
    @BindView(R.id.toolbar_bottom_sheet) Toolbar toolbarBottomSheet;

    @BindView(R.id.tap_action_layout) RelativeLayout tapActionLayout;
    private BottomSheetBehavior mBottomSheetBehavior;


    public ArticoliInColloBottomSheetHelper(AppCompatActivity context, FragmentArticoliInColloBottomSheetBinding binding){
        mActivity = context;
        mBinding = binding;

        ButterKnife.bind(this, mBinding.getRoot());

        mBottomSheetBehavior = BottomSheetBehavior.from(mBinding.getRoot());
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        mBottomSheetBehavior.setBottomSheetCallback(this);

        appBarLayoutBottomSheet.setVisibility(View.INVISIBLE);

        toolbarBottomSheet.setNavigationIcon(R.drawable.ic_close_24dp);
        toolbarBottomSheet.setNavigationOnClickListener(view -> {
            if(mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });

        tapActionLayout.setOnClickListener(v -> {
            if(mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_COLLAPSED) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });


        mBinding.articoliInColloCloseCollo.setText(mActivity.getText(R.string.action_close_ul));
        updateRigheNumber(0);

        mBinding.articoliInColloSheetButton.setOnClickListener(view -> {
            if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        });

    }

    public void initCollo(MtbColt mtbColt){

        mBinding.articoliInColloTitle.setText(String.format(mActivity.getText(R.string.articoli_in_collo_sheet_title).toString(), mtbColt.getNumCollo()));
        mBinding.articoliInColloDetailsDate.setText(mtbColt.getDataColloHumanLong());


        mBinding.articoliInColloDetailsPosizione.setText(mtbColt.getPosizione() != null ? mtbColt.getPosizione() : "N/A");
//        bindings.articoliInColloDetailsPreparatoDa.setText(mtbColt.getPreparatoDa() != null ? mtbColt.getPreparatoDa() : "N/A");

    }

    public void updateRigheNumber(int newRigheNumber){
        mBinding.articoliInColloSheetButton.setText(newRigheNumber + " " + mActivity.getResources().getQuantityString(R.plurals.articles, newRigheNumber));
    }


    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        switch (newState) {
            case BottomSheetBehavior.STATE_HIDDEN:
//                mRootView.scrollTo(0, 0);
                break;
            case BottomSheetBehavior.STATE_EXPANDED:
                appBarLayoutBottomSheet.setClickable(true);
                appBarLayoutBottomSheet.setFocusable(true);
                tapActionLayout.setClickable(false);
                tapActionLayout.setFocusable(false);
                break;
            case BottomSheetBehavior.STATE_COLLAPSED:
                appBarLayoutBottomSheet.setVisibility(View.INVISIBLE);
                appBarLayoutBottomSheet.setClickable(false);
                appBarLayoutBottomSheet.setFocusable(false);
                tapActionLayout.setClickable(true);
                tapActionLayout.setFocusable(true);
                break;
            case BottomSheetBehavior.STATE_DRAGGING:
                appBarLayoutBottomSheet.setVisibility(View.VISIBLE);
//                mRootView.scrollTo(0, 0);
                break;
            case BottomSheetBehavior.STATE_SETTLING:
                appBarLayoutBottomSheet.setVisibility(View.VISIBLE);
//                mRootView.scrollTo(0, 0);
                break;
        }
    }

    public boolean isExpanded() {
        return mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED;
    }

    public void expand() {
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    public void collapse() {
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        appBarLayoutBottomSheet.setAlpha(slideOffset);
        tapActionLayout.setAlpha(1-slideOffset);
    }

}
