package com.xunao.effectdemo.yzs;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.xunao.effectdemo.R;

public class YZSConfigActivity extends Activity implements View.OnClickListener {
  private ConfigBean configBean;
  private CheckBox stOralEvalMode;
  private CheckBox tvVadEnable;
  private CheckBox tvMP3Audio;
  private CheckBox tvAsyncRecognize;
  private CheckBox tvBufferLog;
  private EditText etVadBeforeMs;
  private EditText etVadAfterMs;
  private EditText etScoreAdjuest;
  private EditText etUid;
  private EditText etOnlineIp;
  private EditText etSecret;
  private EditText etSocketTimeout;
  private TextView tvServiceType;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_config);
    configBean = (ConfigBean) getIntent().getSerializableExtra(YZSDemoActivity.CONFIG);
    stOralEvalMode = (CheckBox) findViewById(R.id.stOralEvalMode);
    tvVadEnable = (CheckBox) findViewById(R.id.tvVadEnable);
    tvMP3Audio = (CheckBox) findViewById(R.id.tvMP3Audio);
    tvAsyncRecognize = (CheckBox) findViewById(R.id.tvAsyncRecognize);
    tvBufferLog = (CheckBox) findViewById(R.id.tvBufferLog);
    etVadBeforeMs = (EditText) findViewById(R.id.etVadBeforeMs);
    etVadAfterMs = (EditText) findViewById(R.id.etVadAfterMs);
    etScoreAdjuest = (EditText) findViewById(R.id.etScoreAdjuest);
    etUid = (EditText) findViewById(R.id.etUid);
    etOnlineIp = (EditText) findViewById(R.id.etOnlineIp);
    etSecret = (EditText) findViewById(R.id.etSecret);
    etSocketTimeout = (EditText) findViewById(R.id.etSocketTimeout);
    tvServiceType = (TextView) findViewById(R.id.tvServiceType);
    tvServiceType.setOnClickListener(this);
    findViewById(R.id.tvBack).setOnClickListener(this);
    updateView();
  }

  private void updateView() {
    if (configBean == null) {
      configBean = new ConfigBean();
    }
    stOralEvalMode.setChecked(!configBean.isCN());
    tvVadEnable.setChecked(configBean.isVadEnable());
    tvMP3Audio.setChecked(configBean.isMp3Audio());
    tvAsyncRecognize.setChecked(configBean.isSetAsyncRecognize());
    tvBufferLog.setChecked(configBean.isBufferLog());
    etVadBeforeMs.setText(String.valueOf(configBean.getVadBeforeMs()));
    etVadAfterMs.setText(String.valueOf(configBean.getVadAfterMs()));
    etScoreAdjuest.setText(String.valueOf(configBean.get_scoreAdjuest()));
    etUid.setText(String.valueOf(configBean.getUid()));
    etOnlineIp.setText(String.valueOf(configBean.getOnline_ip()));
    etSecret.setText(String.valueOf(configBean.getSecret()));
    etSocketTimeout.setText(String.valueOf(configBean.getSocket_timeout()));
    tvServiceType.setText(String.valueOf(configBean.getServiceType()));
  }

  private void setConfigBean() {
    if (configBean == null) {
      configBean = new ConfigBean();
    }
    configBean.setCN(!stOralEvalMode.isChecked());
    configBean.setVadEnable(tvVadEnable.isChecked());
    configBean.setMp3Audio(tvMP3Audio.isChecked());
    configBean.setSetAsyncRecognize(tvAsyncRecognize.isChecked());
    configBean.setBufferLog(tvBufferLog.isChecked());
    if (TextUtils.isEmpty(etVadBeforeMs.getText().toString())) {
      etVadBeforeMs.setText("1000");
    }
    configBean.setVadBeforeMs(Integer.parseInt(etVadBeforeMs.getText().toString()));
    if (TextUtils.isEmpty(etVadAfterMs.getText().toString())) {
      etVadAfterMs.setText("1000");
    }
    configBean.setVadAfterMs(Integer.parseInt(etVadAfterMs.getText().toString()));
    if (TextUtils.isEmpty(etScoreAdjuest.getText().toString())) {
      etScoreAdjuest.setText("1.0");
    }
    configBean.set_scoreAdjuest(Float.parseFloat(etScoreAdjuest.getText().toString()));
    configBean.setUid(etUid.getText().toString());
    configBean.setOnline_ip(etOnlineIp.getText().toString());
    configBean.setSecret(etSecret.getText().toString());
    if (TextUtils.isEmpty(etSocketTimeout.getText().toString())) {
      etSocketTimeout.setText("0");
    }
    configBean.setSocket_timeout(Integer.parseInt(etSocketTimeout.getText().toString()));
    configBean.setServiceType(tvServiceType.getText().toString());
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.tvBack:
        setConfigBean();
        Intent intent = new Intent();
        intent.putExtra(YZSDemoActivity.CONFIG, configBean);
        setResult(1001, intent);
        finish();
        break;
      case R.id.tvServiceType:
        newDialog();
        break;
      default:
        break;
    }
  }

  private void newDialog() {
    final CharSequence[] items =
        { "A", "B", "C", "D", "E", "enstar", "gzedunet", "gzedunet_answer" };
    int index = 0;
    for (int i = 0; i < items.length; i++) {
      if (tvServiceType.getText().toString().equals(items[i])) {
        index = i;
      }
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(YZSConfigActivity.this);
    builder.setTitle("评测模式")
        .setCancelable(false)
        .setSingleChoiceItems(items, index, new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            tvServiceType.setText(String.valueOf(items[which]));
            dialog.dismiss();
          }
        });
    AlertDialog dlg = builder.create();
    dlg.show();
  }
}
