package com.gs.apidemo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gs.account.api.BaseAccountApi;
import com.gs.account.context.AccountContext;
import com.gs.account.entity.SipAccount;
import com.gs.account.listener.AccountStatusListener;

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

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

    private TextView accountsTv;
    private String res = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accounts);
        accountsTv = (TextView)findViewById(R.id.tv_res);


        startMonitorAccountChange();
    }

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

        stopMonitorAccountChange();
    }

    public void getAllAccount(View view) {
        List<SipAccount> sipAccounts = BaseAccountApi.getAllSipAccounts();
        Log.i(TAG, "getAllAccount: " + sipAccounts);

        //show accounts on UI
        if(sipAccounts != null && sipAccounts.size() > 0){
            res = "";
            for (int i=0; i<sipAccounts.size();i++){
                SipAccount sipAccount = sipAccounts.get(i);
                if(i>3){
                    res += "...";
                    break;
                }
                res += "("+sipAccount.getAccountId()+")"+"["+sipAccount.getAccountName()+"] \n";
            }
            accountsTv.setText(res);
        } else {
            Toast.makeText(this,"Add a account First!",Toast.LENGTH_LONG).show();
            Intent intent = new Intent(this,UpdateAccountActivity.class);
            startActivityForResult(intent, UpdateAccountActivity.REQ_ADD);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == UpdateAccountActivity.REQ_ADD && resultCode == 1){
            res = "Added ";
        }else if(requestCode == UpdateAccountActivity.REQ_UPDATE && resultCode == 1){
            res = "Updated ";
        }

        if(data!= null){
            int sipAccountId = data.getIntExtra(UpdateAccountActivity.SIP_ACCOUNT_ID,-1);
            if(sipAccountId > -1){
                SipAccount sipAccount = (SipAccount) BaseAccountApi.getAccountById(sipAccountId);
                res += "id:"+sipAccount.getAccountId()+",name:"+sipAccount.getAccountName()+",number:"
                        +sipAccount.getAccountNumber()+",sip server:"+sipAccount.getSipServer()
                        +",sip auth id:"+sipAccount.getSipAuthId()+",sip auth pwd:"+sipAccount.getSipAuthPassword();
            }
            accountsTv.setText(res);
        }

    }

    /////////////////////////////////////////////////////
    //
    // Account Monitor
    //
    /////////////////////////////////////////////////////
    private void startMonitorAccountChange() {
        mAccountStatusListener = new AccountStatusListener();
        BaseAccountApi.addStatusListener("com.gs.apidemo.MonitorAccount",
                mAccountStatusListener.callback,
                AccountContext.ListenType.SIP_ACCOUNT_STATUS, false);
    }

    private void stopMonitorAccountChange() {
        BaseAccountApi.removeStatusListener(mAccountStatusListener.callback);
        mAccountStatusListener.destroy();
        mAccountStatusListener = null;
    }

    private class MyAccountStatusListener extends AccountStatusListener {
        @Override
        public void onSipAccountStatusChanged(List<SipAccount> list, SipAccount sipAccount) {
            // do something
            Log.i(TAG, "sipAccount " + sipAccount);
        }
    };

    AccountStatusListener mAccountStatusListener = null;



    /////////////////////////////////////////////////////
    //
    // Update Account
    //
    /////////////////////////////////////////////////////
    public void onClickSelectAccount(View view) {
        final List<SipAccount> sipAccounts = BaseAccountApi.getAllSipAccounts();
        if (sipAccounts == null || sipAccounts.size() == 0) {
            Toast.makeText(this, "need add account first.", Toast.LENGTH_SHORT).show();
        }

        List<String> accountNames = new ArrayList<String>(sipAccounts.size());
        for (int i = 0; i < sipAccounts.size(); i++) {
            SipAccount account = sipAccounts.get(i);
            accountNames.add(account.getAccountId()+"["+account.getAccountName()+"]");
        }

        final String[] items = accountNames.toArray(new String[]{});
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(this);
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(AccountsActivity.this, "selected: " + items[which], Toast.LENGTH_SHORT).show();
                res="select accountId:"+items[which];
                SipAccount sipAccount = sipAccounts.get(which);
                int wantAccountId = sipAccount.getAccountId();

                Intent intent = new Intent(AccountsActivity.this,UpdateAccountActivity.class);
                intent.putExtra(UpdateAccountActivity.SIP_ACCOUNT_ID,wantAccountId);
                startActivityForResult(intent,UpdateAccountActivity.REQ_UPDATE);
            }
        });
        listDialog.show();
    }
}
