package com.decoding.twodtg.barcodereader2dtg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import decoding.twodtg.com.twodtgdecodelib2.BarcodeDecoderLibrary;

public class ActivationActivity extends Activity
{
  private static final int OnlineAct = 1;
  private static int operation = 0;

  private TextView caption1;
  private TextView caption2;
  private EditText edit1;
  private EditText edit2;
  private Button button;
  private BarcodeDecoderLibrary lib = new BarcodeDecoderLibrary();
  private AlertDialog alertDialog = null;
  private boolean ok = false;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activation);

    caption1 = (TextView) this.findViewById(R.id.caption1);
    caption2 = (TextView) this.findViewById(R.id.caption2);
    edit1 = (EditText) this.findViewById(R.id.edit1);
    edit2 = (EditText) this.findViewById(R.id.edit2);

    if(operation == OnlineAct)
    {
      caption1.setText("License Id");
      caption2.setText("Password");
      edit2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }
  }

  public void ButtonClick(View v)
  {
    if(operation == OnlineAct)
    {
      final int id = Integer.parseInt(edit1.getText().toString());
      final String psw = edit2.getText().toString();
      final ActivationActivity t = this;
      Thread activateThread = new Thread(new Runnable()
      {
        @Override
        public void run()
        {
          final boolean res = lib.OnlineActivation(t, id, psw);
          t.runOnUiThread(new Runnable()
          {
            @Override
            public void run()
            {
              if(res)
              {
                MessageBox("Activation successful!", "OK");
                ok = true;
              }
              else
              {
                MessageBox("Activation failed!", "Error!");
              }
            }
          });
        }
      });
      activateThread.start();
    }
  }

  public static void ToOnlineActivation(Activity owner)
  {
    operation = OnlineAct;
    Intent intent = new Intent(owner, ActivationActivity.class);
    owner.startActivity(intent);
  }

  private void MessageBox(String msg, String caption)
  {
    try
    {
      final Activity t = this;
      alertDialog = new AlertDialog.Builder(this).create();
      alertDialog.setTitle(caption);
      alertDialog.setMessage(msg);
      alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
          if(ok)
            MainActivity.Create(t);
        }
      });
      alertDialog.getWindow().setGravity(Gravity.BOTTOM);
      alertDialog.show();
    }
    catch(Exception e)
    {
    }
  }
}
