package com.kdanmobile.kdan_others_library_for_android.dialog.rateus;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Space;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.kdanmobile.kdan_others_library_for_android.R;

/**
 * Created by Wei on 2017/7/7.
 */

public class RateUsDialog {

    private Context context;
    private Runnable onPositive;
    private Runnable onNegative;
    private Runnable onDismiss;

    public RateUsDialog(Context context) {
        this.context = context;
    }

    public RateUsDialog setOnPositive(Runnable onPositive) {
        this.onPositive = onPositive;
        return this;
    }

    Runnable getOnPositive() {
        return onPositive;
    }

    public RateUsDialog setOnNegative(Runnable onNegative) {
        this.onNegative = onNegative;
        return this;
    }

    public RateUsDialog setOnDismiss(Runnable onDismiss){
        this.onDismiss = onDismiss;
        return this;
    }

    public void show() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_rate_us, null);
        TextView contentTV = (TextView) view.findViewById(R.id.tv_rateUsDialog_content);

        // Only en, zh-tw, zh-cn need to show title
        Space space = (Space) view.findViewById(R.id.space_rate_us_title_content);
        String title = context.getString(R.string.rate_us_dialog_title);
        String content = context.getString(R.string.rate_us_dialog_content);
        contentTV.setText(content);
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
                .customView(view, true)
                .positiveText(R.string.rate_us_dialog_positive)
                .negativeText(R.string.rate_us_dialog_negative)
                .onPositive((dialog, which) -> {
                    if (onPositive != null)  onPositive.run();
                })
                .onNegative((dialog, which) -> {
                    if (onNegative != null) onNegative.run();
                })
                .dismissListener(dialogInterface -> {
                    if (onDismiss != null) onDismiss.run();
                })
                ;
        if (!TextUtils.isEmpty(title)) {
            builder.title(title);
            space.setVisibility(View.GONE);
        }
        builder.show();
    }
}
