package ch.pizzalink.android.helper;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;

import ch.pizzalink.android.R;
import ch.pizzalink.android.activity.BaseActivity;
import ch.pizzalink.android.activity.LoginActivity;

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.navy)
                .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.navy)
                .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.navy)
                .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.navy)
                .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.navy)
                .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){
        new MaterialDialog.Builder(context)
                .title(R.string.alert)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.navy)
                .positiveText(R.string.ok)
                .onPositive(positiveButtonCallback)
                .negativeText(R.string.cancel)
                .onNegative(negativeButtonCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .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.navy)
                .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.pizzalink.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){
        new MaterialDialog.Builder(BaseActivity.currentActivity)
                .title(R.string.app_name)
                .content(message)
                .titleColorRes(R.color.red)
                .contentColorRes(R.color.navy)
                .positiveText(R.string.ok)
                .onPositive(singleButtonCallback)
                .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.navy)
                .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.navy)
                .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.navy)
                .items(itemList)
                .itemsCallback(listCallback)
                .typeface("Quicksand-Medium.ttf", "Quicksand-Regular.ttf")
                .show();
    }

}
