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

import { LocalStorageService } from '@ucap/ng-web-storage';

import { environment } from '@environments';

import { UserStore } from '@app/models/user-store';
import { AppKey } from '@app/types/app-key.type';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Component({
  selector: 'app-pages-account-login',
  templateUrl: './login.page.component.html',
  styleUrls: ['./login.page.component.scss']
})
export class LoginPageComponent implements OnInit, OnDestroy {
  companyGroupCode = environment.companyConfig.companyGroupCode;
  userStore: UserStore;

  readonly useRememberMe =
    environment.productConfig.authentication.useRememberMe;

  readonly useAutoLogin = environment.productConfig.authentication.useAutoLogin;

  readonly fixedCompanyCode = environment.companyConfig.fixedCompanyCode;

  private ngOnDestroySubject = new Subject<boolean>();

  constructor(private localStorageService: LocalStorageService) {}

  ngOnInit(): void {
    this.ngOnDestroySubject = new Subject<boolean>();

    this.localStorageService
      .encGet$<UserStore>(
        AppKey.UserStore,
        environment.productConfig.localEncriptionKey
      )
      .pipe(takeUntil(this.ngOnDestroySubject))
      .subscribe((userStore) => (this.userStore = userStore));
  }

  ngOnDestroy(): void {
    if (!!this.ngOnDestroySubject) {
      this.ngOnDestroySubject.complete();
    }
  }
}
