package com.example.lenovo.addressbook;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import yuku.ambilwarna.AmbilWarnaDialog;

import static android.app.Activity.RESULT_OK;

public class AddPerson extends AppCompatActivity {
    TextView text;
    View button;
    int color = 0xffff0000;
    private DatabaseManager dbManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_person);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        dbManager = DatabaseManager.getInstance(this);

        button = findViewById(R.id.colorButton);
        text = (TextView) findViewById(R.id.color);
        displayColor();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDialog(false);
            }
        });
    }
    void openDialog(boolean supportsAlpha) {
        AmbilWarnaDialog dialog = new AmbilWarnaDialog(AddPerson.this, color, supportsAlpha, new AmbilWarnaDialog.OnAmbilWarnaListener() {
            @Override
            public void onOk(AmbilWarnaDialog dialog, int color) {
                //Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
                AddPerson.this.color = color;
                displayColor();
            }

            @Override
            public void onCancel(AmbilWarnaDialog dialog) {
                //Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }

    void displayColor() {
        text.setTextColor(color);
        button.setBackgroundColor(color);
    }
    //为主行动器设置关联的工具栏菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //为工具栏实例化菜单
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    //菜单事件响应
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //只要在AndroidManifest.xml文件制定了父行动器，工具栏将自动处理返回按钮
        int id = item.getItemId();
        if (id == R.id.finish) {
            new AlertDialog.Builder(AddPerson.this)
                    .setTitle("您是否确认需要添加该联系人？")
                            .setPositiveButton("是",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Person person = new Person();
                                            person.setName(((TextView) findViewById(R.id.name)).getText().toString());
                                            person.setPhone(((TextView) findViewById(R.id.phone)).getText().toString());
                                            person.setEmail(((TextView) findViewById(R.id.email)).getText().toString());
                                            person.setWeixin(((TextView) findViewById(R.id.weixin)).getText().toString());
                                            person.setQq(((TextView) findViewById(R.id.qq)).getText().toString());
                                            person.setOrganization(((TextView) findViewById(R.id.organization)).getText().toString());
                                            person.setAddress(((TextView) findViewById(R.id.address)).getText().toString());
                                            person.setColor(String.format("#%06X", (0xFFFFFF & ((TextView) findViewById(R.id.color)).getCurrentTextColor())));
                                            dbManager.add(person);
                                            Intent intent = new Intent();
                                            intent.putExtra("Action", "Add");
                                            AddPerson.this.setResult(RESULT_OK, intent);
                                            finish();
                                        }
                                    })
                    .setNegativeButton("否", null)
            .show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void showMessage(String message) {
        Toast.makeText(AddPerson.this, message, Toast.LENGTH_SHORT).show();
    }
}
