package it.integry.integrywmsnative.view.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.AppCompatImageView;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;

import it.integry.integrywmsnative.R;

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

public class DialogSimpleMessageHelper {

    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 static AlertDialog makeBase(TYPE type, Context mContext, String titleText, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick, Runnable onNegativeClick, @StringRes int rNeutralButtonString, Runnable onNeutralClick){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        int colorBackgroundTitle = -1;
        Drawable titleIconRes = null;

        switch (type){
            case INFO:
                colorBackgroundTitle = ContextCompat.getColor(mContext, R.color.colorPrimary);
                titleIconRes = mContext.getResources().getDrawable(R.drawable.ic_info_78dp);
                break;

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

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

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

        //Title VIEW
        View titleView = inflater.inflate(R.layout.dialog_custom_header, null);
        TextView title = titleView.findViewById(R.id.title_text);
        title.setText(titleText);

        AppCompatImageView civ = titleView.findViewById(R.id.title_icon);
        ColorStateList colorStateList = ColorStateList.valueOf(colorBackgroundTitle);
        civ.setImageTintList(colorStateList);
        civ.setImageDrawable(titleIconRes);

        //Content View
        View contentView = inflater.inflate(R.layout.dialog_custom_content, null);
        TextView contentMessage = contentView.findViewById(R.id.dialog_content_main_message);
        contentMessage.setText(messageText);

        LinearLayout hashMapContainer = contentView.findViewById(R.id.dialog_content_hashmap);

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

                RelativeLayout singleMapContent = (RelativeLayout) inflater.inflate(R.layout.dialog_custom_content_hashmap_viewmodel, null);
                ((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);
            }
        }


        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext)
                .setCustomTitle(titleView)
                .setView(contentView)
//                .setMessage(messageText)
                .setCancelable(false)
                .setPositiveButton(mContext.getText(R.string.ok), (dialog, which) -> {
                    if(onPositiveClick != null) onPositiveClick.run();
                });

        if(onNeutralClick != null){
            alertDialog.setNeutralButton(rNeutralButtonString, (dialog, which) -> onNeutralClick.run());
        }
        if(onNegativeClick != null){
            alertDialog.setNegativeButton(mContext.getText(R.string.abort), (dialog, which) -> onNegativeClick.run());
        }

        AlertDialog alert = alertDialog.create();
        alert.setCanceledOnTouchOutside(false);

        return alert;
    }


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


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


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


    public static AlertDialog makeWarningDialog(Context mContext, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick, Runnable onNegativeClick){
        return makeBase(TYPE.WARNING,
                mContext,
                mContext.getText(R.string.warning).toString(),
                messageText,
                hashmapContent,
                onPositiveClick,
                () -> { if(onNegativeClick != null) onNegativeClick.run(); }, -1, null);
    }


    public static AlertDialog makeWarningDialog(Context mContext, Spanned messageText, HashMap<String, String> hashmapContent, Runnable onPositiveClick){
        return makeBase(TYPE.WARNING,
                mContext,
                mContext.getText(R.string.warning).toString(),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }

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

        return makeBase(TYPE.ERROR,
                mContext,
                mContext.getText(R.string.ops).toString(),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, -1, null);
    }

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

        return makeBase(TYPE.ERROR,
                mContext,
                mContext.getText(R.string.ops).toString(),
                messageText,
                hashmapContent,
                onPositiveClick,
                null, rNeutralButtonString, onNeutralClick);

    }



}
