import { Component, OnInit } from "@angular/core";
import { CompanyModel } from "../../models/company";
import { HostingInfoModel } from "../../models/hosting-info";
import { DatabaseInfoModel } from "../../models/database-info";
import { ServerInfoModel } from "../../models/server-info";
import { CompanyService } from "../../services/company.service";
import { HostingInfoService } from "../../services/hosting-info.service";
import { DatabaseInfoService } from "../../services/database-info.service";
import { ServerInfoService } from "../../services/server-info.service";

@Component({
  selector: "app-company-information",
  templateUrl: "./company-information.component.html",
  styleUrls: ["./company-information.component.scss"],
})
export class CompanyInformationComponent implements OnInit {
  company: CompanyModel;
  hostingInfo: HostingInfoModel;
  databaseInfo: DatabaseInfoModel;
  serverInfo: ServerInfoModel;
  companyLocalStorage: any;
  timeDiff: any;

  constructor(
    private companyService: CompanyService,
    private hostingInfoService: HostingInfoService,
    private databaseInfoService: DatabaseInfoService,
    private serverInfoService: ServerInfoService
  ) {
    this.companyLocalStorage = JSON.parse(
      localStorage.getItem("companyData") || "{}"
    );

    this.companyService
      .getCompanyById(this.companyLocalStorage.id)
      .subscribe((company) => {
        this.company = company;
      });

    this.hostingInfoService
      .getHostingInfoById(this.companyLocalStorage.id)
      .subscribe((hostingInfo) => {
        this.hostingInfo = hostingInfo;

        this.timeDiff = this.calculateTimeDiff(this.hostingInfo.expirationDate);
      });

    this.databaseInfoService
      .getDatabaseInfoById(this.companyLocalStorage.id)
      .subscribe((databaseInfo) => {
        this.databaseInfo = databaseInfo;
      });

    this.serverInfoService
      .getServerInfoById(this.companyLocalStorage.id)
      .subscribe((serverInfo) => {
        this.serverInfo = serverInfo;
      });
  }

  ngOnInit(): void {}

  // Calculate time difference between current time and expiration time
  calculateTimeDiff(date: any) {
    const currentDate = new Date();
    const hostingInfoDate = new Date(date);
    const diff = hostingInfoDate.getTime() - currentDate.getTime();
    const resultInMinutes = Math.round(diff / 60000);
    const resultInHours = Math.round(diff / 3600000);
    const resultInDays = Math.round(diff / 86400000);
    const resultInWeeks = Math.round(diff / 604800000);
    const resultInMonths = Math.round(diff / 2628000000);
    const resultInYears = Math.round(diff / 31536000000);

    if (resultInMinutes < 60) {
      return resultInMinutes + " minutes left";
    } else if (resultInHours < 24) {
      return resultInHours + " hours left";
    } else if (resultInDays < 7) {
      return resultInDays + " days  left";
    } else if (resultInWeeks < 4) {
      return resultInWeeks + " weeks  left";
    } else if (resultInMonths < 12) {
      return resultInMonths + " months  left";
    } else {
      return resultInYears + " years  left";
    }
  }
}
