package com.decoding.twodtg.barcodereader2dtg;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import decoding.twodtg.com.twodtgdecodelib2.BarcodeDecoderLibrary;

public class IntroActivity extends Activity
{
  private Timer timer;
  private MyTimerTask mMyTimerTask;
  private TextView txt2;
  private TextView txt3;
  private TextView txt4;
  private Button activButton;
  private Button activManualButton;
  private BarcodeDecoderLibrary lib = new BarcodeDecoderLibrary();

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

    txt2 = (TextView) this.findViewById(R.id.textView2);
    txt3 = (TextView) this.findViewById(R.id.textView3);
    txt4 = (TextView) this.findViewById(R.id.textView4);
    activButton = (Button) this.findViewById(R.id.buttonActivationOnline);
    activManualButton = (Button) this.findViewById(R.id.buttonActivationManual);

      boolean act = lib.isActivated(this);
      if(!act)
      {
        txt2.setText("Trial - " + lib.EvaluationDayCount(this) + " days left");
        txt3.setVisibility(View.INVISIBLE);
        ((ViewGroup)(txt4.getParent())).removeView(txt4);
      }
      else
      {
        ((ViewGroup)(activButton.getParent())).removeView(activButton);
        ((ViewGroup)(activManualButton.getParent())).removeView(activManualButton);
      }

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    if(size.y  < 700)
    {
      try
      {
        ImageView logo = (ImageView) this.findViewById(R.id.imageView);
        ((ViewManager) logo.getParent()).removeView(logo);
      }catch(Exception e)
      {}
    }

  }

  static public String SiteUrl = "http://2dtg.com/products/mobile-barcode-decoding/mobile-barcode-decoder-android";
  //static public String SiteUrl = "http://2dtg.com/products/mobile-barcode-decoding/bluetooth-barcode-reader-android";

  public void UrlClick(View v)
  {
    CloseTimer();
    if(lib.isIndastrialVersion() && !lib.isActivated(this))
    {
      ActivationActivity.ToOnlineActivation(this);
    }
    else
    {
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SiteUrl));
      startActivity(browserIntent);
    }
  }

  public void ActivationOnlineClick(View v)
  {
    CloseTimer();
    ActivationActivity.ToOnlineActivation(this);
  }

  public void ActivationManualClick(View v)
  {
    CloseTimer();
    ActivationManualActivity.Create(this);
  }

  public void Click(View v)
  {
    if(lib.isActivated(this) || (lib.EvaluationDayCount(this) > 0))
    {
      MainActivity.Create(this);
    }
  }

  @Override
  protected void onResume()
  {
    super.onResume();
    timer = new Timer();
    mMyTimerTask = new MyTimerTask();
    timer.schedule(mMyTimerTask, 5000);
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    CloseTimer();
  }

  private void CloseTimer()
  {
    if (timer != null)
    {
      timer.cancel();
      timer.purge();
      timer = null;
    }
  }

  class MyTimerTask extends TimerTask
  {
    @Override
    public void run()
    {
      runOnUiThread(new Runnable()
      {
        @Override
        public void run()
        {
          Click(null);
        }
      });
    }
  }
}
