package com.zhang.keybox;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class EditorActivity extends AppCompatActivity {

    public static final String DATA_RETURN = "com.zhang.keybox.return";

    EditText editTextName;
    EditText editTextCount;
    EditText editTextSecret;
    EditText editTextRemark;
    KeyBoxLab mKeyBoxLab;

    Toolbar mToolbar;
    UUID uuid;
    KeyBox keyBox;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor);

        mKeyBoxLab = KeyBoxLab.getKeyBoxLab(EditorActivity.this);
        editTextName = (EditText)findViewById(R.id.editor_name);
        editTextCount = (EditText)findViewById(R.id.editor_count);
        editTextSecret = (EditText)findViewById(R.id.editor_secret);
        editTextRemark = (EditText)findViewById(R.id.editor_remark);
        mToolbar = (Toolbar)findViewById(R.id.editor_toolbar);

        mToolbar.setTitle("编辑");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

     /*从showActivity过来的数据，将其设置到editText上*/
        Intent intent1 = getIntent();
        uuid = (UUID)intent1.getSerializableExtra(ShowActivity.EXTRA_ID);
        if(uuid != null){
            keyBox = mKeyBoxLab.getKeyBox(uuid);
            editTextName.setText(keyBox.getName());
            editTextCount.setText(keyBox.getCount());
            editTextSecret.setText(keyBox.getPassword());
            editTextRemark.setText(keyBox.getRemark());
        }
    }

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.editor_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.editor_ok:
                if(uuid == null) {
                    //如果是从MainActivity启动的话，直接添加信息
                    setInfo();
                }else{
                    //如果是从ShowActivity启动的话，就返回更新的信息
                    keyBox = getupdateKeyBox(keyBox);
                    mKeyBoxLab.updateKeyBox(keyBox);
                    Intent intent2 = new Intent();
                    intent2.putExtra(DATA_RETURN,uuid);
                    setResult(RESULT_OK,intent2);
                    finish();
                }
            case android.R.id.home:
                finish();
            default:
        }
        return true;
    }

    private void setInfo(){
        String name = editTextName.getText().toString();
        String count = editTextCount.getText().toString();
        String secret = editTextSecret.getText().toString();
        String remark = editTextRemark.getText().toString();

        KeyBox keyBox2 = new KeyBox();

        keyBox2.setName(name);
        keyBox2.setCount(count);
        keyBox2.setPassword(secret);
        keyBox2.setRemark(remark);

        mKeyBoxLab.addKeyBox(keyBox2);

        finish();
    }

    private KeyBox getupdateKeyBox(KeyBox keyBox1){

        String name = editTextName.getText().toString();
        String count = editTextCount.getText().toString();
        String secret = editTextSecret.getText().toString();
        String remark = editTextRemark.getText().toString();

        keyBox1.setName(name);
        keyBox1.setCount(count);
        keyBox1.setPassword(secret);
        keyBox1.setRemark(remark);
        return keyBox1;
    }

}

@@ -2834,32 +2834,55 @@
               %7D%0A
+                break;%0A
             case
@@ -2919,32 +2919,55 @@
       finish();%0A
+                break;%0A
             defa
@@ -3268,32 +3268,221 @@
 ().toString();%0A%0A
+        if(name.length() == 0 %7C%7C count.length() == 0 %7C%7C secret.length() == 0)%7B%0A            Toast.makeText(EditorActivity.this,%22%E5%90%8D%E7%A7%B0%EF%BC%8C%E8%B4%A6%E6%88%B7%E5%92%8C%E5%AF%86%E7%A0%81%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%22,Toast.LENGTH_SHORT).show();%0A        %7Delse %7B%0A    
         KeyBox k
@@ -3505,32 +3505,36 @@
 Box();%0A%0A        
+    
 keyBox2.setName(
@@ -3540,32 +3540,36 @@
 (name);%0A        
+   
+ 
 keyBox2.setCount
@@ -3569,32 +3569,36 @@
 etCount(count);%0A
+    
         keyBox2.
@@ -3618,32 +3618,36 @@
 ecret);%0A        
+  
+  
 keyBox2.setRemar
@@ -3654,24 +3654,28 @@
 k(remark);%0A%0A
+    
         mKey
@@ -3702,32 +3702,36 @@
 Box2);%0A%0A        
+ 
+   
 finish();%0A    %7D%0A
@@ -3720,24 +3720,34 @@
   finish();%0A
+        %7D%0A
     %7D%0A%0A    p

添加判断输入的内容是否为空的功能。