package it.integry.integrywmsnative.view.dialogs.versamento_automatico_ul_done;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import org.jetbrains.annotations.NotNull;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.rest.model.VersamentoAutomaticoULResponseDTO;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.databinding.DialogVersamentoAutomaticoUlDoneBinding;

public class DialogVersamentoAutomaticoULDoneView extends BaseDialogFragment {

    public final VersamentoAutomaticoULResponseDTO versamentoAutomaticoULResponseDTO;
    private final Runnable mOnDismiss;

    private DialogVersamentoAutomaticoUlDoneBinding mBindings;

    public static DialogVersamentoAutomaticoULDoneView newInstance(@NotNull VersamentoAutomaticoULResponseDTO versamentoAutomaticoULResponseDTO, @NotNull Runnable onDismiss) {
        return new DialogVersamentoAutomaticoULDoneView(versamentoAutomaticoULResponseDTO, onDismiss);
    }

    private DialogVersamentoAutomaticoULDoneView(@NotNull VersamentoAutomaticoULResponseDTO versamentoAutomaticoULResponseDTO, @NotNull Runnable onDismiss) {
        super();

        this.versamentoAutomaticoULResponseDTO = versamentoAutomaticoULResponseDTO;
        this.mOnDismiss = onDismiss;
    }

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

        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        UtilityDialog.setTo95PercentWidth(requireContext(), this);

        mBindings.setView(this);

        this.initTitle();
        this.initContent();

        return mBindings.getRoot();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

        if (mOnDismiss != null) {
            this.mOnDismiss.run();
        }
    }


    private void initTitle() {
        int colorBackgroundTitle = -1;

        if (versamentoAutomaticoULResponseDTO.getFailed() == null || versamentoAutomaticoULResponseDTO.getFailed().isEmpty()) {
            //SUCCESS
            colorBackgroundTitle = ContextCompat.getColor(requireContext(), R.color.green_300);
        } else {
            //WARNING
            colorBackgroundTitle = ContextCompat.getColor(requireContext(), R.color.yellow_600);
        }


        mBindings.base.setBackgroundColor(colorBackgroundTitle);
    }

    private void initContent() {
        String text;

        if (versamentoAutomaticoULResponseDTO.getFailed() == null || versamentoAutomaticoULResponseDTO.getFailed().isEmpty()) {
            //SUCCESS
            text = "Versamento completato con successo";
        } else {
            //WARNING
            text = "Il sistema non è riuscito a versare <b>" + versamentoAutomaticoULResponseDTO.getFailed().size() +
                    " " + getResources().getQuantityString(R.plurals.articles, versamentoAutomaticoULResponseDTO.getFailed().size()) + "</b>";
        }

        mBindings.descriptionText.setText(Html.fromHtml(text));




//        var notCompletedListAdapter = new BaseAdapter() {
//
//            @Override
//            public View getView(int position, View convertView, ViewGroup parent) {
//
//                var mtbColr = versamentoAutomaticoULResponseDTO.getFailed().get(position);
//
//                var inflater = LayoutInflater.from(requireContext());
//                DialogVersamentoAutomaticoUlDoneNotCompletedListItemBinding binding = null;
//
//                if(convertView == null) {
//                    binding = DataBindingUtil.inflate(inflater, R.layout.dialog_versamento_automatico_ul_done__not_completed_list_item, parent, false);
//                } else {
//                    binding = DataBindingUtil.findBinding(convertView);
//                }
//
//                binding.setItem(mtbColr);
//
//                convertView = binding.getRoot();
//
//                //Setting qty with unt_mis
//                if (!SettingsManager.iDB().isFlagForceAllToColli() && (mtbColr.getMtbAart() == null || mtbColr.getMtbAart().isFlagQtaCnfFissaBoolean())) {
//                    String text = UtilityNumber.decimalToString(mtbColr.getQtaCol());
//
//
//                    if (mtbColr.getMtbAart() != null) {
//                        text += !UtilityString.isNullOrEmpty(mtbColr.getMtbAart().getUntMis()) ? "\n" + mtbColr.getMtbAart().getUntMis() : "";
//                    }
//
//                    binding.qtaTextview.setText(text);
//                } else {
//                    binding.qtaTextview.setText(UtilityNumber.decimalToString(mtbColr.getNumCnf()) + "\n" + UtilityResources.getString(R.string.unt_mis_col));
//                }
//
//                return convertView;
//            }
//
//            @Override
//            public long getItemId(int position) {
//                return position;
//            }
//
//            @Override
//            public Object getItem(int position) {
//                return position;
//            }
//
//            @Override
//            public int getCount() {
//                return versamentoAutomaticoULResponseDTO.getFailed().size();
//            }
//        };
//
//
//        mBindings.detailsNotCompletedList.setAdapter(notCompletedListAdapter);

    }


//    public void toggleDetailVisibility() {
//        mBindings.detailsContainer.toggle(true);
//    }


}
