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 DeactivationActivity extends Activity
{
  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_deactivation);

    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);
    button = (Button) this.findViewById(R.id.buttonDeactivate);

    caption1.setText("License Id");
    caption2.setText("Password");
    edit2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  }

  public void ButtonClick(View v)
  {
      button.setEnabled(false);
      final int id = Integer.parseInt(edit1.getText().toString());
      final String psw = edit2.getText().toString();
      final DeactivationActivity t = this;
      Thread deactivateThread = new Thread(new Runnable()
      {
        @Override
        public void run()
        {
          final boolean res = lib.OnlineDeactivation(t, id, psw);
          t.runOnUiThread(new Runnable()
          {
            @Override
            public void run()
            {
              if(res)
              {
                MessageBox("Deactivation successful!", "OK");
                ok = true;
              }
              else
              {
                MessageBox("Deactivation on server failed!", "Error!");
                button.setEnabled(true);
              }
            }
          });
        }
      });
      deactivateThread.start();
  }

  public static void ToOnlineDeactivation(Activity owner)
  {
    Intent intent = new Intent(owner, DeactivationActivity.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)
          {
            Intent intent = new Intent(t, IntroActivity.class);
            t.startActivity(intent);
          }
        }
      });
      alertDialog.getWindow().setGravity(Gravity.BOTTOM);
      alertDialog.show();
    }
    catch(Exception e)
    {
    }
  }
}
