package com.decoding.twodtg.barcodereader2dtg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import decoding.twodtg.com.twodtgdecodelib2.BarcodeDecoderLibrary;

import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class ActivationManualActivity extends Activity
{
  private static final String userCodesFileName = "userCodesFile";
  private EditText uc1;
  private EditText uc2;
  private EditText ac1;
  private EditText ac2;
  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_manual);
    uc1 = (EditText) this.findViewById(R.id.editUserCode1);
    uc2 = (EditText) this.findViewById(R.id.editUserCode2);
    ac1 = (EditText) this.findViewById(R.id.editActivationCode1);
    ac2 = (EditText) this.findViewById(R.id.editActivationCode2);

    uc1.setFocusable(false);
    uc2.setFocusable(false);

    try
    {
      BufferedReader userCodesFile = new BufferedReader(new InputStreamReader(openFileInput(userCodesFileName)));
      String s = userCodesFile.readLine();
      StringTokenizer tokens = new StringTokenizer(s, " ");
      String s1 = tokens.nextToken();
      String s2 = tokens.nextToken();
      uc1.setText(s1);
      uc2.setText(s2);
      userCodesFile.close();
    }
    catch(Exception e)
    {
      try
      {
        OutputStreamWriter userCodesFile = new OutputStreamWriter(openFileOutput(userCodesFileName, Context.MODE_PRIVATE));
        String s1 = lib.UserCode1(this);
        String s2 = lib.UserCode2(this);
        uc1.setText(s1);
        uc2.setText(s2);
        userCodesFile.write(s1);
        userCodesFile.write(" ");
        userCodesFile.write(s2);
        userCodesFile.close();
      }catch(Exception e1)
      {}
    }

  }

  public static void Create(Activity a)
  {
    Intent intent = new Intent(a, ActivationManualActivity.class);
    a.startActivity(intent);
  }

  public void ButtonManualActivateClick(View v)
  {
    try
    {
      int c1 = Integer.parseInt(uc1.getText().toString());
      int c2 = Integer.parseInt(uc2.getText().toString());
      int a1 = Integer.parseInt(ac1.getText().toString());
      int a2 = Integer.parseInt(ac2.getText().toString());

      if(lib.ManualActivation(this, a1, a2, c1, c2))
      {
        MessageBox("Activation successful!", "OK");
        ok = true;
      } else
      {
        ActFailed();
      }
    }catch(Exception e)
    {
      ActFailed();
    }
  }

  private void ActFailed()
  {
    MessageBox("User or activation codes incorrect. Please try again.", "Error!");
  }

  @Override
  protected void onResume()
  {
    super.onResume();
  }

  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.getWindow().setGravity(Gravity.BOTTOM);
      alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
          if(ok)
            MainActivity.Create(t);
        }
      });
      alertDialog.show();
    }
    catch(Exception e)
    {
    }
  }
}
