/*
 * Copyright (c) 2018, FusionAuth, All Rights Reserved
 */
'use strict';

var FusionAuth = FusionAuth || {};
FusionAuth.OAuth2 = FusionAuth.OAuth2 || {};

/**
 * @constructor
 */
FusionAuth.OAuth2.Authorize = function() {
  Prime.Utils.bindAll(this);

  this.form = Prime.Document.queryFirst('form[action="authorize"]');
  this.device = this.form.queryFirst('input[name="device"]');
  this.deviceName = this.form.queryFirst('input[name="metaData.device.name"]');
  this.deviceType = this.form.queryFirst('input[name="metaData.device.type"]');
  this.timezone = this.form.queryFirst('input[name="timezone"]');

  this.deviceName.setValue(Prime.Browser.os + ' ' + Prime.Browser.name);
  this.deviceType.setValue('BROWSER');
  this.timezone.setValue(jstz.determine().name());

  if (Prime.Storage.supported) {
    var fingerprint = localStorage.getItem('fusionAuth.device');
    if (fingerprint == null) {
      new Fingerprint2().get(this._handleFingerprint2Callback);
    } else {
      this.device.setValue(fingerprint);
    }
  }
};

FusionAuth.OAuth2.Authorize.constructor = FusionAuth.OAuth2.Authorize;
FusionAuth.OAuth2.Authorize.prototype = {

  /* ===================================================================================================================
   * Private methods
   * ===================================================================================================================*/

  _handleFingerprint2Callback: function(result) {
    this.device.setValue(result);
    localStorage.setItem('fusionAuth.device', result);
  }
};
