package com.gs.apidemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.gs.account.api.BaseAccountApi;
import com.gs.account.entity.SipAccount;

public class UpdateAccountActivity extends BaseActivity {
    private static final String TAG = UpdateAccountActivity.class.getSimpleName();

    public static final String SIP_ACCOUNT_ID = "sipAccountId";
    public static final int REQ_ADD = 1;
    public static final int REQ_UPDATE = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_account);
        Button saveBtn = (Button) findViewById(R.id.update);

        Intent intent = getIntent();
        int accountId = intent.getIntExtra(SIP_ACCOUNT_ID,-1);
        if(accountId != -1){
            SipAccount sipAccount = (SipAccount) BaseAccountApi.getAccountById(accountId);
            setAccount(R.id.tv_account_id,sipAccount.getAccountId()+"");
            setAccount(R.id.tv_group_id,sipAccount.getGroupId()+"");
            setAccount(R.id.tv_account_name,sipAccount.getAccountName());
            setAccount(R.id.tv_account_number,sipAccount.getAccountName());
            setAccount(R.id.tv_sip_server,sipAccount.getAccountName());
            setAccount(R.id.tv_sip_auth_id,sipAccount.getAccountName());
            setAccount(R.id.tv_sip_auth_pwd,sipAccount.getSipAuthPassword());
        }else{
            saveBtn.setText("Add");
        }
    }

    private String getAccountInfo(int resId){
        EditText tv= (EditText)findViewById(resId);
            return tv.getText().toString();
    }

    private void setAccount(int resId,String info){
        EditText tv= (EditText)findViewById(resId);
        tv.setText(info);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public void addAccount(View view) {
        String accountID = getAccountInfo(R.id.tv_account_id);
        String groupID = getAccountInfo(R.id.tv_group_id);
        String name = getAccountInfo(R.id.tv_account_name);
        String number = getAccountInfo(R.id.tv_account_number);
        String sipServer = getAccountInfo(R.id.tv_sip_server);
        String sipAuthId = getAccountInfo(R.id.tv_sip_auth_id);
        String sipPwd = getAccountInfo(R.id.tv_sip_auth_pwd);

        int accountId = Integer.parseInt(accountID);
        int groupId = Integer.parseInt(groupID);

        SipAccount sipAccount = new SipAccount.Builder().setAccountId(accountId).setGroupId(groupId)
                .setAccountName(name).setAccountNumber(number)
                .setSipServer(sipServer).setSipAuthId(sipAuthId)
                .setSipAuthPwd(sipPwd).build();

        BaseAccountApi.updateSipAccount(sipAccount);
        Intent intent = new Intent();

        intent.putExtra(SIP_ACCOUNT_ID,accountId);
        setResult(1,intent);
        finish();
    }





}
