package net.ddns.mahaljsp.ch13_01_sqlite;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Calendar;
import java.util.Date;

public class UpdateActivity extends Activity {
    TextView txtId, txtBirthday;
    ItemDAO itemDAO;
    EditText editFirstName, editLastName, editTel, editEmail, editAddress;
    Intent intent;
    long _id;
    int mYear=1980, mMonth=0, mDay=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        processViews();
        processControllers();
    }
    public void processViews(){
        txtId=(TextView)findViewById(R.id.txtId);
        editFirstName=(EditText)findViewById(R.id.editFirstName);
        editLastName=(EditText)findViewById(R.id.editLastName);
        editTel=(EditText)findViewById(R.id.editTel);
        editEmail=(EditText)findViewById(R.id.editEmail);
        editAddress=(EditText)findViewById(R.id.editAddress);
        txtBirthday=(TextView)findViewById(R.id.txtBirthday);
    }
    public void processControllers(){
        itemDAO=new ItemDAO(this);
        intent= getIntent();
        _id=intent.getLongExtra("ID", -1);
        if (_id!=-1) {
            Item item = itemDAO.get(_id);
            txtId.setText(String.valueOf(item.getId()));
            editFirstName.setText(item.getFirstname());
            editLastName.setText(item.getLastname());
            editTel.setText(item.getTel());
            editEmail.setText(item.getEmail());
            editAddress.setText(item.getAddress());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(item.getBirthday()));
            mYear = calendar.get(Calendar.YEAR);
            mMonth = calendar.get(Calendar.MONTH);
            mDay = calendar.get(Calendar.DATE);
            txtBirthday.setText(mYear + "/" + (mMonth + 1) + "/" + mDay);
        }
    }
    public void btOk_click(View view){
        Item item=new Item();
        item.setFirstname(editFirstName.getText().toString());
        item.setLastname(editLastName.getText().toString());
        item.setTel(editTel.getText().toString());
        item.setEmail(editEmail.getText().toString());
        item.setAddress(editAddress.getText().toString());
        Calendar calendar=Calendar.getInstance();
        calendar.set(mYear, mMonth, mDay);
        item.setBirthday(calendar.getTimeInMillis());
        if(_id==-1)
            itemDAO.insert(item);
        else{
            item.setId(_id);
            itemDAO.update(item);
        }
        setResult(Activity.RESULT_OK, intent);
        this.finish();
    }
    public void btCancel_click(View view){
        this.finish();
    }
    public void btDate_click(View view){
        DatePickerDialog dialogDate=new DatePickerDialog(UpdateActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        mYear = year;
                        mMonth = month;//month為年的第幾月, 第一個月為0
                        mDay = day;
                        txtBirthday.setText(mYear+"/"+mMonth+"/"+mDay);
                    }
                }, mYear, mMonth, mDay);
        dialogDate.show();
    }
}
