package com.example.memosystem.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.memosystem.MainActivity;
import com.example.memosystem.R;
import com.example.memosystem.activity.listen.ChangeListen;
import com.example.memosystem.dao.AdminDao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

public class AddActivity extends AppCompatActivity {
    String uuid=null;
    String account=null;

    EditText res=null;
    EditText title=null;

    TextView textView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        //加载菜单条
        Toolbar toolbar = findViewById(R.id.toolbar_add);
        setSupportActionBar(toolbar);

        //获取账号
        Intent intent=getIntent();



        textView= findViewById(R.id.add_time);
        //获取当前月份和时间
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
        String curTime=dateFormat.format(calendar.getTime());

        curTime=curTime+"   |   ";



        curTime=curTime+"0字";
        textView.setText(curTime);


        //是根据下面输入下面文本框的操作时间来定的
       res=findViewById(R.id.add_con);//输入内容的
        title=findViewById(R.id.add_title);//输入内容的


        //判断是添加界面还是更改界面
        account=intent.getStringExtra("account");//获取当前账号
        String a=intent.getStringExtra("sta");
        if(a==null){//是添加界面
            uuid = UUID.randomUUID().toString();//随机产生的一个UUID
        }else{
            intent.putExtra("sta","1");

            uuid=intent.getStringExtra("id");
            res.setText( intent.getStringExtra("con"));
            title.setText( intent.getStringExtra("title"));
            textView.setText(intent.getStringExtra("time"));

        }

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent(AddActivity.this,ManageActivity.class);
                intent1.putExtra("account",account);
                startActivity(intent1);
            }
        });




        res.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //获取当前月份和时间
                Calendar calendar=Calendar.getInstance();
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
                String curTime=dateFormat.format(calendar.getTime());

                curTime=curTime+"   |   ";

                String text= String.valueOf(res.getText());
                String zi=String.valueOf(text.length());
                curTime=curTime+zi+"字";
                textView.setText(curTime);
                //每次输入一个字符，他都自动保存一下
                String titles=title.getText().toString().trim();
                String rest=res.getText().toString().trim();
                int a=new AdminDao().saveRecord(uuid, titles,rest,account,curTime);

            }
        });





    }

    //构造菜单


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

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        int itemID=item.getItemId();
        if(itemID==R.id.add_men_save){//保存

            String curTime=textView.getText().toString().trim();
            String titles=title.getText().toString().trim();
            String rest=res.getText().toString().trim();
            int a=new AdminDao().saveRecord(uuid, titles,rest,account,curTime);
            if(a==1){
                Toast.makeText(AddActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(AddActivity.this, "保存失败！", Toast.LENGTH_SHORT).show();
            }

        }else
        if(itemID==R.id.add_men_del){//删除

            int a=new AdminDao().delRecord(uuid,account);
            Intent intent=new Intent(AddActivity.this,ManageActivity.class);
            intent.putExtra("account",account);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
}