package com.decoding.twodtg.demobarcodereader2dtg;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
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)
    {
      AskPermissions();
    }
  }

  private void Activation()
  {
    final int id;
    final String psw;
    try
    {
      id = Integer.parseInt(edit1.getText().toString());
      psw = edit2.getText().toString();
    }catch(Exception e)
    {
      MessageBox("Please set licens ID and PSW", "Warning!");
      return;
    }
    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();
  }

  private void AskPermissions()
  {
    if(android.os.Build.VERSION.SDK_INT < 23)
    {
      return;
    }else
    {
      ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.INTERNET}, 0);
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
  {
    for(int i=0; i < grantResults.length; i++)
      if(grantResults[i] != 0)
        return;
    Activation();
  }

  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)
    {
    }
  }
}
