package com.gs.apidemo;

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

import com.gs.account.api.BaseAccountApi;
import com.gs.account.entity.SipAccount;
import com.gs.phone.api.audio.AudioRouteApi;
import com.gs.phone.api.call.BaseCallApi;
import com.gs.phone.context.PhoneContext;
import com.gs.phone.entity.DialResults;
import com.gs.phone.entity.DialingInfo;

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

/*
 */
public class DialerActivity extends BaseActivity {
    private static final String TAG = DialerActivity.class.getSimpleName();
    private TextView accountTv;
    private SipAccount mSelectedAccount = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_dialer);
        mSelectedAccount = null;
        accountTv = (TextView) findViewById(R.id.tv_account);
    }

    public void startCall(View view) {
        EditText editText = (EditText)findViewById(R.id.et_number);
        String num = editText.getText().toString();
        if (!TextUtils.isEmpty(num)) {
            call(num);
        }
    }

    public void call(String num) {
        Log.i(TAG, "call: " + num);
        if (mSelectedAccount == null) {
            Log.i(TAG, "need to select an account.");
            return;
        }
        List<DialingInfo> dialingInfos = new ArrayList<DialingInfo>();
        DialingInfo dialingInfo = new DialingInfo();
        dialingInfo.setAccountId(mSelectedAccount.getAccountId());
        dialingInfo.setOriginNumber(num);
        dialingInfos.add(dialingInfo);
        DialResults dialResults = BaseCallApi.makeCalls(dialingInfos);
        int result = dialResults.getDialResult(dialingInfo);
        if (result != PhoneContext.CallErrorCode.SUCCESS) {
            Log.i(TAG, "call: Failed " + result);
        } else {
            Log.i(TAG, "call: Calling!");
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                AudioRouteApi.switchVoiceToSpeaker();


                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                AudioRouteApi.switchVoiceToHdmi();


                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                AudioRouteApi.switchVoiceToBlueToothHeadset();

            }
        }).start();
    }

    public void endCall(View view) {
        Log.i(TAG, "endCall");
        DemoApplication app = (DemoApplication) getApplication();
        int lineId = app.getCurLineId();
        if (lineId != -1) {
            BaseCallApi.endCall(lineId);
        }
    }

    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.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(DialerActivity.this, "selected: " + items[which], Toast.LENGTH_SHORT).show();
                accountTv.setText(items[which]);
                mSelectedAccount = sipAccounts.get(which);
            }
        });
        listDialog.show();
    }
}
