package tech.fuge.www.forgotfirstlinecode.chapter3;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tech.fuge.www.forgotfirstlinecode.R;

public class Main3ActivityDialog extends AppCompatActivity {

    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;
    @BindView(R.id.btn5)
    Button btn5;
    @BindView(R.id.btn6)
    Button btn6;
    @BindView(R.id.btn7)
    Button btn7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3_dialog);
        ButterKnife.bind(this);
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, Main3ActivityDialog.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                myDialog();
                break;
            case R.id.btn2:
                myDialogWithItems();
                break;
            case R.id.btn3:
                myDialogWithSingleChoice();
                break;
            case R.id.btn4:
                myDialogWithMultiChoice();
                break;
            case R.id.btn5:
                myDialogWithCustomView();
                break;
            case R.id.btn6:
                myDialogWithTwoButton();
                break;
            case R.id.btn7:
                myDialogWithThreeButton();
                break;
            default:break;
        }
    }

    public void myDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("简单的dialog")
                .setIcon(R.drawable.ic_android_black_24dp)
                .setMessage("这是一个简单的dialog");
        Dialog dialog = builder.create();
        dialog.show();
    }

    public void myDialogWithItems() {
//        final String[] strs = {"11", "22", "33", "44", "55"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("带Items的dialog, 并设置点击外部无效，此时不能用setMessage属性,否则setItems就失效了")
                .setIcon(R.drawable.ic_android_black_24dp)
//                .setMessage("这是一个简单带Items的dialog")
                .setItems(R.array.dialogItem, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Main3ActivityDialog.this, "preses" + strs[which], Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", null)
                .setCancelable(false)
                .create()
                .show();
//        Dialog dialog = builder.create();
//        dialog.show();
    }

    String[] strs = {"单选1", "单选2", "单选3", "单选4", "单选5"};

    /**
     * 单选对话框
     */
    public void myDialogWithSingleChoice() {
//         strs = {"单选1", "单选2", "单选3", "单选4", "单选5"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("一个单选dialog, 并设置点击外部无效")
                .setIcon(R.drawable.ic_android_black_24dp)
                //下方的0代表默认选中第一项，若1则是默认第二项，而是超出数量的数字如-1，则是默认没有选中项
                .setSingleChoiceItems(R.array.singleChoice, -1, mySingleChiceOnClickListener)
//                .setSingleChoiceItems(R.array.singleChoice, 0, new myDialogWithSingleChiceOnClickListener())
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        which = mySingleChiceOnClickListener.getWhich();
                        String curStr = getResources().getStringArray(R.array.singleChoice)[which];
                        Snackbar.make(btn1, "确定选择了" + curStr, Snackbar.LENGTH_SHORT).show();
                    }
                })
                .setCancelable(false)
                .create()
                .show();

    }

    boolean[] multiInitBool = {false, false, false, false, false};

    public void myDialogWithMultiChoice() {
//         strs = {"单选1", "单选2", "单选3", "单选4", "单选5"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("一个多dialog, 并设置点击外部无效")
                .setIcon(R.drawable.ic_android_black_24dp)
                //下方的multiInitBool代表默认选中哪些项目
                .setMultiChoiceItems(R.array.multiChoice, multiInitBool, new myDialogWithMultiChiceOnClickListener())
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MyDialogActivity.this, "你选择了" + getResources().getStringArray(R.array.multiChoice)[which], Toast.LENGTH_SHORT).show();
//                        Snackbar.make(btn1, "确定"+getResources().getStringArray(R.array.multiChoice)[which], Snackbar.LENGTH_SHORT).show();
                    }
                })
                .setCancelable(false)
                .create()
                .show();

    }

    public void myDialogWithCustomView() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
////        LayoutInflater inflater = getLayoutInflater();
//        View layout = getLayoutInflater().inflate(R.layout.toolbar_demo_content,null);
//        builder.setTitle("一个自定义view的对话框")
//                .setIcon(R.drawable.ic_arrow_back_black_24dp)
//                .setView(layout)
//                .create()
//                .show();
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.activity_main3_dialog_custom);
        dialog.show();
    }

    public void myDialogWithTwoButton() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("包含两个按钮的对话框")
                .setIcon(R.drawable.ic_android_black_24dp)
                .setMessage("内容:是否退出登录?")
                .setPositiveButton("退出", null)
                .setNegativeButton("取消", null)
                .create()
                .show();
    }

    public void myDialogWithThreeButton() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("包含三个按钮的对话框")
                .setIcon(R.drawable.ic_android_black_24dp)
                .setMessage("内容:检测到新版本，是否更新？")
                .setPositiveButton("更新", null)
                .setNeutralButton("稍后更新",null)
                .setNegativeButton("此版本不再提醒", null)
                .create()
                .show();
    }

    myDialogWithSingleChiceOnClickListener mySingleChiceOnClickListener = new myDialogWithSingleChiceOnClickListener();


    /**
     * 单选对话框的监听器
     */
    private class myDialogWithSingleChiceOnClickListener implements DialogInterface.OnClickListener {
        private int mWhich = 0;

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mWhich = which;
            String strWhich = getResources().getStringArray(R.array.singleChoice)[which];
            Toast.makeText(Main3ActivityDialog.this, "你选择了" + strWhich, Toast.LENGTH_SHORT).show();
        }

        public int getWhich() {
            return mWhich;
        }
    }

    private class myDialogWithMultiChiceOnClickListener implements DialogInterface.OnMultiChoiceClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            String strWhich = getResources().getStringArray(R.array.multiChoice)[which];
            Toast.makeText(Main3ActivityDialog.this, "你选择了" + strWhich, Toast.LENGTH_SHORT).show();
        }
    }
}
