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",
      },
    };

    // license-checker --production --json --out license.json
    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);
      }
    });
  }
}
