package jp.gihyo.mitoma.android.s010901transition;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class FriendsAction {
    private Activity act;

    public FriendsAction(Activity act){

        this.act = act;
    }
    public void exec(View view) {
        // 画面データの所得
        EditText etId = act.findViewById(R.id.etId);
        EditText etName = act.findViewById(R.id.etName);
        EditText etPhone = act.findViewById(R.id.etPhone);
        String id = etId.getText().toString();
        String name = etName.getText().toString();
        String phone = etPhone.getText().toString();

        // ボタンのタグ取得
        String tag = view.getTag().toString();
        FriendsDAO dao = FriendsDAO.getInstance();

        // 登録処理
        if (tag.equals("REGISTER")) {
            FriendsDTO dto = new FriendsDTO(id, name, phone, "");
            dao.insert(dto);
            Toast.makeText(act, "登録完了", Toast.LENGTH_LONG).show();

        // 照会処理
        } else if (tag.equals("ENQUIRY")) {
            FriendsDTO dto = dao.select(id);
            if (dto != null) {
                etName.setText(dto.name);
                etPhone.setText(dto.phone);
            } else {
                etName.setText("(該当データ無し)");
                etPhone.setText("");
            }
            Toast.makeText(act, "照会完了", Toast.LENGTH_LONG).show();

        // 更新処理
        } else if (tag.equals("UPDATE")) {
            FriendsDTO dto = new FriendsDTO(id, name, phone, "");
            dao.update(dto);
            Toast.makeText(act, "更新完了", Toast.LENGTH_LONG).show();

        // 削除処理
        } else if (tag.equals("DELETE")) {
            dao.delete(id);
            Toast.makeText(act, "削除完了", Toast.LENGTH_LONG).show();
        }
    }
}
