package ch.pizzalemon.android.helper;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;
import ch.pizzalemon.android.activity.LoginActivity;
import ch.pizzalemon.android.activity.RegisterActivity;

public class DialogHelper {

    private static MaterialDialog loadingDialog;

    public static void showDialogWithPositiveButton(Context context, String message) {
        showDialogWithPositiveButton("", context, message);
    }

    public static void showDialogWithPositiveButton(String title, Context context, String message) {

        if(title.isEmpty())
            title = context.getString(R.string.app_name);

        new MaterialDialog.Builder(context)
                .title(title)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .positiveColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        //BaseActivity.currentActivity.onBackPressed();
                        dialog.dismiss();
                    }
                })
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();

    }


    public static void showAlertDialog(Context context, String content) {

        if(context == null)
            return;

        new MaterialDialog.Builder(context)
                .title(R.string.alert)
                .content(content)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .positiveColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showFailedDialog() {

        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.alert)
                .content(BaseActivity.currentActivity.getString(R.string.failed))
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .positiveColor(ContextCompat.getColor(BaseActivity.currentActivity, R.color.colorPrimary))
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showNoNetworkDialog() {

        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.alert)
                .content(BaseActivity.currentActivity.getString(R.string.no_network_message))
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .positiveColor(ContextCompat.getColor(BaseActivity.currentActivity, R.color.colorPrimary))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        BaseActivity.currentActivity.finishAffinity();
                    }
                })
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showLoadingDialog(){
        if(loadingDialog != null && loadingDialog.isShowing())
            return;
        loadingDialog = new MaterialDialog.Builder(BaseActivity.currentActivity)
                .content(BaseActivity.currentActivity.getString(R.string.loading))
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .cancelable(false)
                .progress(true, 0)
                .progressIndeterminateStyle(true)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void hideLoadingDialog(){
        if(loadingDialog != null && loadingDialog.isShowing()){
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    public static void showTwoButtonsDialog(Context context, String message,
                                            MaterialDialog.SingleButtonCallback positiveButtonCallback,
                                            MaterialDialog.SingleButtonCallback negativeButtonCallback){
        showTwoButtonsDialog(
                context,
                message,
                R.string.ok,
                positiveButtonCallback,
                R.string.cancel,
                negativeButtonCallback);
    }

    public static void showTwoButtonsDialog(Context context,
                                            String message,
                                            int positiveTextResId,
                                            MaterialDialog.SingleButtonCallback positiveButtonCallback,
                                            int negativeTextResId,
                                            MaterialDialog.SingleButtonCallback negativeButtonCallback){
        new MaterialDialog.Builder(context)
                .title(R.string.alert)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(positiveTextResId)
                .onPositive(positiveButtonCallback)
                .negativeText(negativeTextResId)
                .onNegative(negativeButtonCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showTwoButtonsDialog(String title,
                                            String message,
                                            int positiveTextResId,
                                            MaterialDialog.SingleButtonCallback positiveButtonCallback,
                                            int negativeTextResId,
                                            MaterialDialog.SingleButtonCallback negativeButtonCallback){
        showTwoButtonsDialog(
                title,
                message,
                positiveTextResId,
                positiveButtonCallback,
                negativeTextResId,
                negativeButtonCallback,
                true);
    }

    public static void showTwoButtonsDialog(String title,
                                            String message,
                                            int positiveTextResId,
                                            MaterialDialog.SingleButtonCallback positiveButtonCallback,
                                            int negativeTextResId,
                                            MaterialDialog.SingleButtonCallback negativeButtonCallback,
                                            boolean isCancelable){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(title)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(positiveTextResId)
                .onPositive(positiveButtonCallback)
                .negativeText(negativeTextResId)
                .onNegative(negativeButtonCallback)
                .cancelable(isCancelable)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showTwoButtonsDialog(String title,
                                            String message,
                                            int positiveTextResId,
                                            MaterialDialog.SingleButtonCallback positiveButtonCallback,
                                            int negativeTextResId,
                                            MaterialDialog.SingleButtonCallback negativeButtonCallback,
                                            boolean isCancelable,
                                            boolean isButtonTextSmallCaps){

        MaterialDialog materialDialog =
                new MaterialDialog.Builder(BaseActivity.currentActivity)
                        .title(title)
                        .content(message)
                        .titleColorRes(R.color.red)
                        .contentColorRes(R.color.black)
                        .positiveText(positiveTextResId)
                        .onPositive(positiveButtonCallback)
                        .negativeText(negativeTextResId)
                        .onNegative(negativeButtonCallback)
                        .cancelable(isCancelable)
                        .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                        .build();
        materialDialog.getActionButton(DialogAction.POSITIVE).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.getActionButton(DialogAction.NEGATIVE).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.getActionButton(DialogAction.NEUTRAL).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.show();
    }


    public static void showNeedToLoginDialog(int messageResourceId){

        MaterialDialog dialog = new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.alert)
                .content(BaseActivity.currentActivity.getString(messageResourceId))
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.button_login)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent loginIntent = new Intent(BaseActivity.currentActivity, LoginActivity.class);
                        loginIntent.putExtra("showBackIcon", true);
                        BaseActivity.currentActivity.startActivity(loginIntent);
                    }
                })
                .negativeText(R.string.register_text)
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        BaseActivity.currentActivity.startActivity(new Intent(BaseActivity.currentActivity, RegisterActivity.class));
                    }
                })
                .neutralText(R.string.cancel_all_caps)
                //.onNeutral()
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .build();

        dialog.getActionButton(DialogAction.POSITIVE).setTextSize(12);
        dialog.getActionButton(DialogAction.NEGATIVE).setTextSize(12);
        dialog.getActionButton(DialogAction.NEUTRAL).setTextSize(12);

        dialog.show();
    }

    public static void showUpdateAppDialog(Context context) {

        new MaterialDialog.Builder(context)
                .title(R.string.alert)
                .content(R.string.alert_update_app)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.update_app)
                .positiveColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .cancelable(false)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse("market://details?id=ch.pizzalemon.android"));
                        BaseActivity.currentActivity.startActivity(intent);
                        BaseActivity.currentActivity.finishAffinity();
                    }
                })
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showOneButtonDialogWithCallback(String message,
                                                       MaterialDialog.SingleButtonCallback singleButtonCallback){
        showOneButtonDialogWithCallback(message, singleButtonCallback, true);
    }


    public static void showOneButtonDialogWithCallback(String message,
                                                       MaterialDialog.SingleButtonCallback singleButtonCallback,
                                                       boolean cancelable){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.app_name)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .onPositive(singleButtonCallback)
                .cancelable(cancelable)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showOneButtonDialogWithCallback(String title,
                                                       String message,
                                                       String positiveButtonText,
                                                       MaterialDialog.SingleButtonCallback singleButtonCallback,
                                                       boolean cancelable,
                                                       boolean isButtonTextSmallCaps){
        MaterialDialog materialDialog =
                new MaterialDialog.Builder(BaseActivity.currentActivity)
                        .title(title)
                        .content(message)
                        .titleColorRes(R.color.red)
                        .contentColorRes(R.color.black)
                        .positiveText(positiveButtonText)
                        .onPositive(singleButtonCallback)
                        .cancelable(cancelable)
                        .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                        .build();
        materialDialog.getActionButton(DialogAction.POSITIVE).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.getActionButton(DialogAction.NEGATIVE).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.getActionButton(DialogAction.NEUTRAL).setAllCaps(!isButtonTextSmallCaps);
        materialDialog.show();
    }

    public static void showOneButtonDialogWithDismissListener(String message,
                                                              DialogInterface.OnDismissListener dismissListener){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.app_name)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .dismissListener(dismissListener)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }


    /*
    public static void showPasswordResetDialog(final Context context) {

        new MaterialDialog.Builder(context)
                .title(R.string.app_name)
                .content(R.string.password_reset)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .positiveColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .cancelable(false)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent intent = new Intent(context, LoginActivity.class);
                        BaseActivity.currentActivity.startActivity(intent);
                        BaseActivity.currentActivity.finishAffinity();
                    }
                })
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();

    }


    public static void showProfileUpdatedDialog(MaterialDialog.SingleButtonCallback singleButtonCallback) {
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.app_name)
                .content(R.string.profile_updated)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .positiveText(R.string.ok)
                .cancelable(false)
                .onPositive(singleButtonCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }
    */

    public static void showListDialog(ArrayList<String> itemList, MaterialDialog.ListCallback listCallback){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.choose)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .items(itemList)
                .itemsCallback(listCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

    public static void showStoreListDialog(ArrayList<String> itemList,
                                           MaterialDialog.ListCallback listCallback){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.choose_store)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.black)
                .items(itemList)
                .itemsCallback(listCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }


}
