package com.itant.shiwushu.tool;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.itant.shiwushu.R;
import com.itant.shiwushu.manager.SharedPreferenceManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.net.GlobalPresenter;
import com.itant.shiwushu.ui.login.LoginActivity;

/**
 * Created by Jason on 2018/10/5.
 */

public class DialogTool {
    /**
     * 是否退出登录
     */
    public static void showExitDialog(final Activity activity) {
        // 退出当前账号
        AlertDialog dialog = new AlertDialog.Builder(activity)
                //.setIcon(R.mipmap.icon)//设置标题的图片
                //.setTitle("我是对话框")//设置对话框的标题
                .setMessage("是否清除当前登录信息并退出？")//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        SystemTool.exit();
                        ActivityTool.startActivity(activity, new Intent(activity, LoginActivity.class));
                        activity.finish();
                    }
                }).create();
        dialog.show();
    }

    /**
     * 意见反馈|举报对话框
     */
    public static void showFeedbackDialog(Activity activity, final String reporterId, final String reporterName, final String authorId, final String authorName) {
        View view = activity.getLayoutInflater().inflate(R.layout.layout_feedback, null);
        final EditText et_feedback = view.findViewById(R.id.et_feedback);
        if (TextUtils.isEmpty(reporterId)) {
            // 不是举报的才显示之前的反馈信息
            String failedFeedback = SharedPreferenceManager.getInstance().getString(SharedPreferenceManager.KEY_FEEDBACK);
            if (!TextUtils.isEmpty(failedFeedback)) {
                et_feedback.setText(failedFeedback);
                et_feedback.setSelection(failedFeedback.length());
            }
        } else {
            et_feedback.setHint("请输入举报的原因");
        }

        // 退出当前账号
        final AlertDialog dialog = new AlertDialog.Builder(activity)
                .setView(view)
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", null).create();
        dialog.show();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feedback = et_feedback.getText().toString();
                if (TextUtils.isEmpty(feedback)) {
                    ToastManager.getInstance().toastShort("请输入内容");
                } else {
                    dialog.dismiss();
                    // 发送反馈
                    GlobalPresenter.submitFeedback(feedback, reporterId, reporterName, authorId, authorName);
                }
            }
        });
    }

    /**
     * 是否开通VIP会员
     */
    public static void showVIPDialog(final Activity activity) {
        AlertDialog dialog = new AlertDialog.Builder(activity)
                //.setIcon(R.mipmap.icon)//设置标题的图片
                .setTitle("开通会员")//设置对话框的标题
                .setMessage("VIP认证会员可以编辑个人资料，提高个人信用度，是否开通？")//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        // 支付宝转账1.99元
                        DonateTool.openAlipayPayPage(activity, "https://qr.alipay.com/fkx05937kvc49ayh1mko112");
                        GlobalPresenter.becomeVIP();
                    }
                }).create();
        dialog.show();
    }
}
