import uuidv4 from 'uuid/v4';
import { oauthClientId } from '../bungie-api/bungie-api-utils';
import template from './login.html';
import './login.scss';

export const LoginComponent = {
  template,
  controller: LoginCtrl,
  controllerAs: 'vm'
};

function LoginCtrl($stateParams) {
  'ngInject';

  const vm = this;

  localStorage.authorizationState = uuidv4();
  const clientId = oauthClientId();

  const reauth = $stateParams.reauth;
  const loginUrl = `/en/OAuth/Authorize?client_id=${clientId}&response_type=code&state=${localStorage.authorizationState}`;

  if (reauth) {
    // TEMPORARY: fully log out from Bungie.net by redirecting to a special logout/relogin page
    // Soon, Bungie.net will respect the reauth parameter and we won't have to do this
    const logoutUrl = `https://www.bungie.net/en/User/SignOut?bru=${encodeURIComponent(loginUrl)}`;
    vm.authorizationURL = logoutUrl;
  } else {
    vm.authorizationURL = `https://www.bungie.net${loginUrl}${reauth ? '&reauth=true' : ''}`;
  }
}


@@ -437,28 +437,56 @@
 th;%0A
+%0A
   
-const loginUrl = %60
+vm.authorizationURL = %60https://www.bungie.net
 /en/
@@ -586,419 +586,8 @@
 ate%7D
-%60;%0A%0A  if (reauth) %7B%0A    // TEMPORARY: fully log out from Bungie.net by redirecting to a special logout/relogin page%0A    // Soon, Bungie.net will respect the reauth parameter and we won't have to do this%0A    const logoutUrl = %60https://www.bungie.net/en/User/SignOut?bru=$%7BencodeURIComponent(loginUrl)%7D%60;%0A    vm.authorizationURL = logoutUrl;%0A  %7D else %7B%0A    vm.authorizationURL = %60https://www.bungie.net$%7BloginUrl%7D
 $%7Bre
@@ -620,11 +620,7 @@
 %7D%60;%0A
-  %7D%0A
 %7D%0A%0A

Remove our explicit logout since reauth works now