import { Subscription } from 'rxjs';

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

import { Store, select } from '@ngrx/store';

import { Company } from '@ucap/api-external';

import { ProtocolService } from '@ucap/ng-protocol';
import { I18nService } from '@ucap/ng-i18n';

import { CompanyActions, CompanySelector } from '@ucap/ng-store-organization';
import { UserStore } from '@app/models/user-store';

@Component({
  selector: 'app-sections-account-login',
  templateUrl: './login.section.component.html',
  styleUrls: ['./login.section.component.scss']
})
export class LoginSectionComponent implements OnInit, OnDestroy {
  @Input()
  companyGroupCode: string;

  @Input()
  userStore: UserStore;

  @Input()
  useRememberMe: boolean;

  @Input()
  useAutoLogin: boolean;

  companyList: Company[];

  private companyListSubscription: Subscription;

  constructor(
    private protocolService: ProtocolService,
    private i18nService: I18nService,
    private store: Store<any>
  ) {}

  ngOnInit(): void {
    this.protocolService.disconnect();

    this.store.dispatch(
      CompanyActions.companies({
        req: { companyGroupCode: this.companyGroupCode }
      })
    );

    this.companyListSubscription = this.store
      .pipe(select(CompanySelector.companyList))
      .subscribe(companyList => {
        this.companyList = companyList;
      });
  }

  ngOnDestroy(): void {
    if (!!this.companyListSubscription) {
      this.companyListSubscription.unsubscribe();
    }
  }

  onLogin(event: {
    companyCode: string;
    loginId: string;
    loginPw: string;
    rememberMe: boolean;
    autoLogin: boolean;
    notValid: () => void;
  }) {}

  onClickForgotPassword() {
    this.i18nService.changeLanguage('ko');
  }
}
