package it.integry.integrywmsnative.gest.prod_fabbisogno_linee_prod.dialog_ask_mag_prossimita;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
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 androidx.databinding.DataBindingUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.OnSingleClickListener;
import it.integry.integrywmsnative.core.expansion.RunnableArgsss;
import it.integry.integrywmsnative.core.model.JtbFasi;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.databinding.DialogAskMagazzinoProssimitaBinding;

public class DialogAskMagazzinoProssimitaView extends BaseDialogFragment implements DialogAskMagazzinoProssimitaViewModel.Listener {

    @Inject
    DialogAskMagazzinoProssimitaViewModel mViewModel;

    private final RunnableArgsss<JtbFasi, Date, Date> mOnItemSelected;

    private Context mContext;
    private DialogAskMagazzinoProssimitaBinding mBindings;

//    private JtbFasi mSelectedItem;

    public static DialogAskMagazzinoProssimitaView newInstance(RunnableArgsss<JtbFasi, Date, Date> onItemSelected) {
        return new DialogAskMagazzinoProssimitaView(onItemSelected);
    }


    private DialogAskMagazzinoProssimitaView(RunnableArgsss<JtbFasi, Date, Date> onItemSelected) {
        super();

        this.mOnItemSelected = onItemSelected;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mContext = getActivity();

        mBindings = DataBindingUtil.inflate(inflater, R.layout.dialog_ask_magazzino_prossimita, container, false);

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

        mBindings.setView(this);
        mBindings.setViewmodel(this.mViewModel);

        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


        mBindings.buttonConfirm.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
//                if(mSelectedItem != null) {
                    getDialog().dismiss();
//                } else {
//                    FBToast.warningToast(mContext, getResources().getString(R.string.please_select_an_item), Toast.LENGTH_LONG);
//                }
            }
        });

        mBindings.buttonAbort.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                getDialog().dismiss();
            }
        });

        mBindings.filledExposedDropdownStartDate.setOnClickListener(onStartDateClickListener);
        mBindings.filledExposedDropdownEndDate.setOnClickListener(onEndDateClickListener);

        return mBindings.getRoot();
    }


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

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

        this.mViewModel.setListener(this);
        this.mViewModel.init(codMdep);

//        this.mViewModel.getJtbFasi().observe(getViewLifecycleOwner(), this::onPosizioniLoaded);
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        mOnItemSelected.run(null, mViewModel.startDate, mViewModel.endDate);
    }

    private final View.OnClickListener onStartDateClickListener = new OnSingleClickListener() {
        @Override
        public void onSingleClick(View v) {
            Calendar c = UtilityDate.getCalendarInstance();


            if(mViewModel.startDateBindable != null) {
                c.setTime(mViewModel.startDate);
            }

            int mYear = c.get(Calendar.YEAR);
            int mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
                    (view2, year, month, day) -> {

                        mViewModel.startDate = new GregorianCalendar(year, month, day).getTime();
                        mViewModel.startDateBindable.set(UtilityDate.formatDate(mViewModel.startDate, UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN));

                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
    };

    private final View.OnClickListener onEndDateClickListener = new OnSingleClickListener() {
        @Override
        public void onSingleClick(View v) {
            Calendar c = UtilityDate.getCalendarInstance();

            if(mViewModel.endDateBindable != null) {
                c.setTime(mViewModel.endDate);
            }

            int mYear = c.get(Calendar.YEAR);
            int mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
                    (view2, year, month, day) -> {

                        mViewModel.endDate = new GregorianCalendar(year, month, day).getTime();
                        mViewModel.endDateBindable.set(UtilityDate.formatDate(mViewModel.endDate, UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN));

                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
    };


//    private void onPosizioniLoaded(List<JtbFasi> items) {
//        DialogAskMagazzinoProssimitaAdapter adapter = new DialogAskMagazzinoProssimitaAdapter(mContext, items);
//
//        AutoCompleteTextView editTextDropdownCliente = mBindings.dropdownLineaProd;
//        editTextDropdownCliente.setThreshold(0);
//        editTextDropdownCliente.setAdapter(adapter);
//        editTextDropdownCliente.setOnItemClickListener((parent, view, position, id) -> {
//            mSelectedItem = items.get(position);
//        });
//    }

}
