import { Component, OnInit } from '@angular/core';

import { HttpClient } from '@angular/common/http';

import packageInfo from 'package.json';
import { EnvironmentService } from 'src/environments/environment.service';
import { CordovaService } from 'src/app/shared/services/cordova.service';

export interface libType {
  licenseFile?: string;
  licenses?: string;
  publisher?: string;
  repository?: string;
  name?: string;
}

@Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.scss'],
})
export class AboutComponent implements OnInit {
  public data: libType[] = [];
  public version: string = packageInfo.version;

  constructor(
    private httpClient: HttpClient,
    public environmentService: EnvironmentService,
    private cordovaService: CordovaService
  ) {}

  ngOnInit() {
    let cordovaLicenses = {
      cordova: {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/apache/cordova',
        publisher: 'apache',
      },
      'cordova-android': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/apache/cordova-android',
        publisher: 'apache',
      },
      'cordova-plugin-file': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/apache/cordova-plugin-file',
        publisher: 'apache',
      },
      'cordova-plugin-device': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/apache/cordova-plugin-device',
        publisher: 'apache',
      },
      'cordova-plugin-local-notifications': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/katzer/cordova-plugin-local-notifications',
        publisher: 'Sebastián Katzer',
      },
      'cordova-clipboard': {
        licenses: 'MIT',
        repository: 'https://github.com/ihadeed/cordova-clipboard',
        publisher: 'Ibby Hadeed',
      },
      'cordova-plugin-deeplinks': {
        licenses: 'MIT',
        repository: 'https://github.com/e-imaxina/cordova-plugin-deeplinks',
        publisher: 'Imaxina Novas Tecnoloxías',
      },
      'cordova-plugin-inappbrowser': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/apache/cordova-plugin-inappbrowser',
        publisher: 'apache',
      },
      'cordova-plugin-android-permissions': {
        licenses: 'Apache-2.0',
        repository: 'https://github.com/NeoLSN/cordova-plugin-android-permissions',
        publisher: 'Jason Yang',
      },
      'cordova-plugin-localization-strings': {
        licenses: 'MIT',
        repository: 'https://github.com/kelvinhokk/cordova-plugin-localization-strings',
        publisher: 'kelvinhokk',
      },
      'cordova-plugin-file-opener2': {
        licenses: 'MIT',
        repository: 'https://github.com/pwlin/cordova-plugin-file-opener2',
        publisher: 'pwlin',
      },
      'cordova-plugin-keyboard': {
        licenses: 'Apache 2.0',
        repository: 'https://github.com/cjpearson/cordova-plugin-keyboard',
        publisher: 'Connor Pearson',
      },
      'cordova-plugin-statusbar': {
        licenses: 'Apache 2.0',
        repository: 'https://github.com/apache/cordova-plugin-statusbar',
        publisher: 'erisu',
      },
    };

    // license-checker --production --json --out license.json
    // do not forget to remove the clarabot from that list
    this.httpClient.get('assets/lib-licenses.json').subscribe((data) => {
      console.log('data', data);
      data = <{ [key: string]: libType }>data;

      for (let key in data) {
        let el = data[key];
        let arr = key.split('@');
        arr.pop();
        let name = arr.join('');
        el['name'] = name;
        this.data.push(el);
      }

      for (let key in cordovaLicenses) {
        let el = cordovaLicenses[key];
        el['name'] = key;
        this.data.push(el);
      }
    });
  }
}
