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

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.DataBindingUtil;

import java.util.HashMap;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.core.utility.UtilityResources;
import it.integry.integrywmsnative.databinding.DialogBaseBinding;

/**
 * Created by GiuseppeS on 22/03/2018.
 */

public class DialogSimpleMessageView extends BaseDialogFragment {

    public enum TYPE {
        INFO(1),
        SUCCESS(2),
        WARNING(3),
        ERROR(4);

        private int value;

        TYPE(int value) {
            this.value = value;
        }

        public int getValue() {
            return this.value;
        }

        public static TYPE fromInt(int value) {
            for (TYPE b : TYPE.values()) {
                if (b.value == value) return b;
            }
            return null;
        }
    }


    private final TYPE mType;
    private final String mTitleText;
    private final Spanned mMessageText;
    private final HashMap<String, String> mHashmapContent;
    private final Runnable mOnPositiveClick;
    private final Runnable mOnNegativeClick;
    private final @StringRes Integer mRNeutralButtonString;
    private final Runnable mOnNeutralClick;


    private String positiveButtonText;
    private String neutralButtonText;
    private String negativeButtonText;


    private DialogBaseBinding mBindings;
    private Context mContext;

    public static DialogSimpleMessageView newInstance(@NonNull TYPE type, @NonNull String titleText, @NonNull Spanned messageText, HashMap<String, String> hashmapContent , Runnable onPositiveClick, Runnable onNegativeClick, @StringRes Integer rNeutralButtonString, Runnable onNeutralClick) {
        return new DialogSimpleMessageView(type, titleText, messageText, hashmapContent, onPositiveClick, onNegativeClick, rNeutralButtonString, onNeutralClick);
    }

    private DialogSimpleMessageView(@NonNull TYPE type, @NonNull String titleText, @NonNull Spanned messageText, HashMap<String, String> hashmapContent , Runnable onPositiveClick, Runnable onNegativeClick, @StringRes Integer rNeutralButtonString, Runnable onNeutralClick) {
        super();

        this.mType = type;
        this.mTitleText = titleText;
        this.mMessageText = messageText;
        this.mHashmapContent = hashmapContent;
        this.mOnPositiveClick = onPositiveClick;
        this.mOnNegativeClick = onNegativeClick;
        this.mRNeutralButtonString = rNeutralButtonString;
        this.mOnNeutralClick = onNeutralClick;
    }

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

        UtilityDialog.setTo90PercentWidth(this.mContext, this);
    }

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

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

        mBindings.setView(this);
        mBindings.setLifecycleOwner(this);

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

        int colorBackgroundTitle = -1;
        Drawable titleIconRes = null;

        switch (mType) {
            case INFO:
                colorBackgroundTitle = ContextCompat.getColor(mContext, R.color.light_blue_300);
                titleIconRes = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_info_78dp, null);
                break;

            case SUCCESS:
                colorBackgroundTitle = ContextCompat.getColor(mContext, R.color.green_300);
                titleIconRes = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_done_white_24dp, null);
                break;

            case WARNING:
                colorBackgroundTitle = ContextCompat.getColor(mContext, R.color.yellow_600);
                titleIconRes = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_warning_white_24dp, null);
                break;

            case ERROR:
                colorBackgroundTitle = ContextCompat.getColor(mContext, R.color.red_300);
                titleIconRes = ResourcesCompat.getDrawable(mContext.getResources(), R.drawable.ic_mood_bad_24dp, null);
                break;
        }

        this.positiveButtonText = mContext.getText(R.string.ok).toString();
        this.negativeButtonText = mContext.getText(R.string.no).toString();

        //Title VIEW
        mBindings.titleText.setText(mTitleText);
        ColorStateList colorStateList = ColorStateList.valueOf(Color.WHITE);
        mBindings.titleIcon.setImageTintList(colorStateList);
        mBindings.titleIcon.setImageDrawable(titleIconRes);

        mBindings.titleContainer.setBackgroundColor(colorBackgroundTitle);

        //Content View
        mBindings.descriptionText.setText(mMessageText);

        LinearLayoutCompat hashMapContainer = mBindings.dialogContentHashmap;

        if (mHashmapContent != null) {
            for (int i = 0; i < mHashmapContent.keySet().size(); i++) {
                String currentKey = mHashmapContent.keySet().toArray()[i].toString();
                String currentValue = mHashmapContent.get(currentKey);

                RelativeLayout singleMapContent = (RelativeLayout) inflater.inflate(R.layout.dialog_custom_content_hashmap_viewmodel, hashMapContainer);
                ((TextView) singleMapContent.findViewById(R.id.dialog_content_hashmap_key)).setText(currentKey);
                ((TextView) singleMapContent.findViewById(R.id.dialog_content_hashmap_value)).setText(currentValue);

                hashMapContainer.addView(singleMapContent);
            }
        }

        return mBindings.getRoot();
    }


    public boolean isPositiveVisible() {
        return mOnPositiveClick != null;
    }

    public boolean isNeutralVisible() {
        return mOnNeutralClick != null;
    }

    public boolean isNegativeVisible() {
        return mOnNegativeClick != null;
    }


    public String getPositiveButtonText() {
        return positiveButtonText;
    }

    public String getNeutralButtonText() {
        return mRNeutralButtonString != null && mRNeutralButtonString != -1 ? mContext.getText(mRNeutralButtonString).toString() : null;
    }

    public String getNegativeButtonText() {
        return negativeButtonText;
    }

    public void onPositiveClick() {
        dismiss();
        if(mOnPositiveClick != null) mOnPositiveClick.run();
    }

    public void onNeutralClick() {
        dismiss();
        if(mOnNeutralClick != null) mOnNeutralClick.run();
    }

    public void onNegativeClick() {
        dismiss();
        if(mOnNegativeClick != null) mOnNegativeClick.run();
    }

    public static DialogSimpleMessageView makeInfoDialog(String titleText, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick) {
        return newInstance(TYPE.INFO,
                titleText,
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }


    public static DialogSimpleMessageView makeInfoDialog(String titleText, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick, Runnable onNegativeClick) {
        return newInstance(TYPE.INFO,
                titleText,
                messageText,
                hashmapContent,
                onPositiveClick,
                onNegativeClick, -1, null);
    }


    public static DialogSimpleMessageView makeSuccessDialog(String titleText, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick) {
        return newInstance(TYPE.SUCCESS,
                titleText,
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }


    public static DialogSimpleMessageView makeWarningDialog(Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick, Runnable onNegativeClick) {
        return newInstance(TYPE.WARNING,
                UtilityResources.getString(R.string.warning),
                messageText,
                hashmapContent,
                onPositiveClick,
                () -> {
                    if (onNegativeClick != null) onNegativeClick.run();
                }, -1, null);
    }


    public static DialogSimpleMessageView makeWarningDialog(Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick) {
        return newInstance(TYPE.WARNING,
                UtilityResources.getString(R.string.warning),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }

    public static DialogSimpleMessageView makeErrorDialog(Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick) {

        return newInstance(TYPE.ERROR,
                UtilityResources.getString(R.string.ops),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }

    public static DialogSimpleMessageView makeErrorDialog(Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick, @StringRes int rNeutralButtonString, Runnable onNeutralClick) {

        return newInstance(TYPE.ERROR,
                UtilityResources.getString(R.string.ops),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, rNeutralButtonString, onNeutralClick);

    }


}
