import { Component, inject, Inject, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { SUPPORTED_CULTURES_TOKEN, SUPPORTED_LANGUAGES_TOKEN } from '../..//injection-tokens';
import * as Cnst from '../../consts';
import { Globals } from '../../services/globals';
import { CoreUser } from '../../models/coreuser';
import { AuthorizeService } from '../../services/authorize.service';
import { RepositoryService } from '../../services/http/repository.service';
import { LocaleService } from '../../services/localization/locale.service';
import { ServerInfoService } from '../../services/serverinfo.service';
import * as Utils from '../../utils';

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss']
})
export class AccountComponent implements OnInit {
  public noneCaption: string = Cnst.EN_DropDownConst4Filter.NoneCaption;
  public noneValue: number = Cnst.EN_DropDownConst4Filter.NoneValueInt;
  public webAppIdList: number[] = [];
  public usedCultures: Cnst.SupportedCulture[];
  public usedLanguages: Cnst.SupportedLanguage[];
  public today: Date;
  public currencySymbol: string;
  public get user(): CoreUser {
    return this.authService.user;
  }

  public globals: Globals = inject(Globals);
  protected router: Router = inject(Router);

  public authService: AuthorizeService = inject(AuthorizeService);
  public serverInfoService: ServerInfoService = inject(ServerInfoService);
  public localeService: LocaleService = inject(LocaleService);
  protected repositoryService: RepositoryService = inject(RepositoryService);
  protected APP_SUPPORTED_CULTURES: string[] = inject(SUPPORTED_CULTURES_TOKEN);
  protected APP_SUPPORTED_LANGUAGES: string[] = inject(SUPPORTED_LANGUAGES_TOKEN);


  constructor(
  ) {
  }


  ngOnInit() {
    this.usedLanguages = Cnst.SUPPORTED_LANGUAGES.filter((lang: Cnst.SupportedLanguage) => Utils.isLanguageSupported(lang.langCode, this.APP_SUPPORTED_LANGUAGES));
    this.usedCultures = Cnst.SUPPORTED_CULTURES.filter((cltr: Cnst.SupportedCulture) => Utils.isCultureSupported(cltr.culture, this.APP_SUPPORTED_CULTURES));
    this.today = Utils.today();
    this.currencySymbol = Utils.getCurrencySymbol(this.localeService.currentCulture);
  }

  logout() {
    this.authService.logout(true);
  }

  setLocale(culture: string) {
    this.localeService.currentCulture = culture;
    this.currencySymbol = Utils.getCurrencySymbol(this.localeService.currentCulture);
    this.saveCultureLanguage();
  }

  setLangauge(lang: string) {
    this.localeService.currentLanguage = lang;
    this.saveCultureLanguage();
  }


  saveCultureLanguage() {
    this.user.culture = this.localeService.currentCulture;
    this.user.language = this.localeService.currentLanguage;
    this.user.save(this.repositoryService, null, null, null, 'culture');
  }


  goHome() {
    this.router.navigate(['/' + this.globals.mainPage.path]);
  }

}
