import { Injectable } from '@angular/core';
import { AppNotificationService } from './notification.service';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { EnviromentUtilService } from '@ucap-webmessenger/util';
import { DeviceType } from '@ucap-webmessenger/core';
import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types';

@Injectable()
export class AppService {
  constructor(
    private enviromentUtilService: EnviromentUtilService,
    private sessionStorageService: SessionStorageService,
    private appNotificationService: AppNotificationService
  ) {}

  public postInit(): Promise<void> {
    return new Promise<void>((resolve, reject) => {
      try {
        let deviceType: DeviceType;
        if (this.enviromentUtilService.nodeWebkit()) {
          deviceType = DeviceType.PC;
        } else if (this.enviromentUtilService.android()) {
          deviceType = DeviceType.Android;
        } else if (this.enviromentUtilService.ios()) {
          deviceType = DeviceType.iOS;
        } else {
          deviceType = DeviceType.Web;
        }

        this.sessionStorageService.set<EnvironmentsInfo>(
          KEY_ENVIRONMENTS_INFO,
          {
            deviceType
          }
        );

        this.appNotificationService.subscribe();
        resolve();
      } catch (error) {
        reject();
      }
    });
  }
}
