/* Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */

import { Component, OnInit } from '@angular/core';
import {XHR} from "./utils/xhr";
import {Loader} from "./utils/loader";
import {NotificationsService} from "./services/notifiaction_service";
import {AppState} from "./app.service";

declare var jQuery: any;

@Component({
  selector: 'license',
  template: require('./../templates/license.component.html'),
  styles: [require('../styles/license.component.less')],
  providers: [XHR]
})
export class LicenseComponent {
  public license_key:string;
  public command = 'cloudlinux-license';
  private _loading;

  constructor(
      public appState: AppState,
      private request: XHR,
      private notifictions: NotificationsService
  ) {}

  public saveLicenseKey(){
    this.loading = true;
    let self = this;
    this.request.post({
      command: this.command,
      method: 'set',
      params: {
        'key': this.license_key,
      },
    }).then((response: any)=>{
        if(response.license == 'valid'){
          location.hash = '/current';
        } else {
          self.loading=false;
          self.notifictions.error('Error', 'License is not valid');
        }
    }).catch((response:any) =>{
        self.loading=false;
    });
  }

  set loading(value: boolean) {
    this._loading = value;
    Loader.__switchBigLoader(jQuery('.license_component'), value);
  }

  get loading() {
    return this._loading;
  }

}
