package com.example.lenovo.addressbook;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import yuku.ambilwarna.AmbilWarnaDialog;

public class DetailActivity extends AppCompatActivity {
    private EditText mName, mPhone, mEmail, mWeixin, mQQ, mOrganization, mAddress,mColor;
    private ImageButton callButton, emailButton;
    private Button mButton;
    private String telNumber;
    private DatabaseManager dbManager;
    private Person person;
    private Long person_id;
    int color = 0xffff0000;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        //获取Intent传入的数据
        dbManager =DatabaseManager.getInstance(this);
        person_id = getIntent().getLongExtra("ID", 0);
        person = dbManager.getPerson(person_id);
        //获取子视图
        mName = (EditText) findViewById(R.id.DETAILS_name);
        mPhone = (EditText) findViewById(R.id.DETAILS_phone);
        mEmail = (EditText) findViewById(R.id.DETAILS_email);
        mWeixin = (EditText) findViewById(R.id.DETAILS_weixin);
        mQQ= (EditText) findViewById(R.id.DETAILS_qq);
        mOrganization = (EditText) findViewById(R.id.DETAILS_organization);
        mAddress = (EditText) findViewById(R.id.DETAILS_address);
        callButton = (ImageButton) findViewById(R.id.callButton);
        emailButton = (ImageButton) findViewById(R.id.emailButton);
        mColor = (EditText)findViewById(R.id.color);
        mButton = (Button) findViewById(R.id.pickColorButton);

        //设置子视图属性值
        mName.setText(person.getName());
        mPhone.setText(person.getPhone());
        telNumber = person.getPhone();
        mEmail.setText(person.getEmail());
        mWeixin.setText(person.getWeixin());
        mQQ.setText(person.getQq());
        mOrganization.setText(person.getOrganization());
        mAddress.setText(person.getAddress());
        color = Color.parseColor(person.getColor());
        displayColor();

        //创建打电话按钮的监听器
        callButton.setOnClickListener(new ImageButton.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                makeCall(telNumber);
            }
        });
    //创建发邮件按钮的监听器
    emailButton.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto",person.getEmail(), null));
            //emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
            startActivity(Intent.createChooser(emailIntent, "发送邮件..."));
            }
        });

        //拾取颜色按钮的监听器
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 openDialog(false);
            }
        });
    }
    void openDialog(boolean supportsAlpha) {
        AmbilWarnaDialog dialog = new AmbilWarnaDialog(DetailActivity.this, color, supportsAlpha, new AmbilWarnaDialog.OnAmbilWarnaListener() {
            @Override
            public void onOk(AmbilWarnaDialog dialog, int color) {
                //Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
                DetailActivity.this.color = color;
        displayColor();
        }

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

        void displayColor() {
        mColor.setTextColor(color);
        mColor.setBackgroundColor(color);
        }
        //为联系人详细信息行动器设置关联的工具栏菜单
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        //为工具栏实例化菜单
        getMenuInflater().inflate(R.menu.menu_detail, menu);
        return true;
        }
        //菜单事件响应
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            //只要在AndroidManifest.xml文件制定了父行动器，工具栏将自动处理返回按键
            int id = item.getItemId();
            //修改
        if (id == R.id.modify) {
            if (item.getTitle().toString().equalsIgnoreCase("修改")){
                 mName.setEnabled(true);
                 mPhone.setEnabled(true);
                 mEmail.setEnabled(true);
                 mWeixin.setEnabled(true);
                 mQQ.setEnabled(true);
                 mOrganization.setEnabled(true);
                 mAddress.setEnabled(true);

                 item.setIcon(R.drawable.finish);
                 item.setTitle("完成");
                 mButton.setVisibility(View.VISIBLE);
            }else {
                 Person person = new Person();
                 person.setId(person_id.intValue());
                 person.setName(((TextView) findViewById(R.id.DETAILS_name)).getText().toString());
                 person.setPhone(((TextView) findViewById(R.id.DETAILS_phone)).getText().toString());
                 person.setEmail(((TextView) findViewById(R.id.DETAILS_email)).getText().toString());
                 person.setWeixin(((TextView) findViewById(R.id.DETAILS_weixin)).getText().toString());
                 person.setQq(((TextView) findViewById(R.id.DETAILS_qq)).getText().toString());
                 person.setOrganization(((TextView) findViewById(R.id.DETAILS_organization)).getText().toString());
                 person.setAddress(((TextView) findViewById(R.id.DETAILS_address)).getText().toString());
                 person.setColor(String.format("#%06X", (0xFFFFFF & ((TextView) findViewById(R.id.color)).getCurrentTextColor())));
                 dbManager.update(person);

                 mName.setEnabled(false);
                 mPhone.setEnabled(false);
                 mEmail.setEnabled(false);
                 mWeixin.setEnabled(false);
                 mQQ.setEnabled(false);
                 mOrganization.setEnabled(false);
                 mAddress.setEnabled(false);
                 item.setIcon(R.drawable.modify);
                 item.setTitle("修改");
                 mButton.setVisibility(View.INVISIBLE);
                 }
                 return true;
        }
        //删除
        if (id == R.id.remove) {
            new AlertDialog.Builder(DetailActivity.this)
                     .setTitle("您是否确认要删除该联系人")
                     .setPositiveButton("是",
                             new DialogInterface.OnClickListener() {
                                 @Override
                                public void onClick(DialogInterface dialog, int which) {
                                     dbManager.remove(person_id);
                                     Intent intent = new Intent();
                                     intent.putExtra("Action", "person removed");
                                     DetailActivity.this.setResult(RESULT_OK, intent);
                                     finish();
                                }
                            })
                    .setNegativeButton("否",null)
                    .show();
           return true;
       }
        return super.onOptionsItemSelected(item);
    }

    //拨打电话
    private void makeCall(String telNumber) {
         Uri uri = Uri.parse("tel:" + telNumber.trim());
         Intent intent = new Intent(Intent.ACTION_CALL, uri);
         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
         startActivity(intent);
        }
    }
}
