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

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

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

import * as AppStore from '@app/store';
import * as AuthenticationStore from '@app/store/account/authentication';
import * as CompanyStore from '@app/store/setting/company';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';

@Component({
  selector: 'app-page-account-login',
  templateUrl: './login.page.component.html',
  styleUrls: ['./login.page.component.scss']
})
export class LoginPageComponent implements OnInit {
  companyList$: Observable<Company[]>;

  constructor(private store: Store<any>, private router: Router) {}

  ngOnInit(): void {
    this.store.dispatch(
      CompanyStore.companyList({
        companyGroupCode: 'LG'
      })
    );

    this.companyList$ = this.store.pipe(
      select(AppStore.SettingSelector.CompanySelector.companyList)
    );
  }

  onLogin(value: {
    companyCode: string;
    loginId: string;
    loginPw: string;
    rememberMe: boolean;
    notValid: () => void;
  }) {
    this.store.dispatch(
      AuthenticationStore.webLogin({
        loginInfo: {
          companyCode: value.companyCode,
          companyGroupType: 'C',
          loginId: value.loginId,
          loginPw: value.loginPw
        },
        rememberMe: value.rememberMe
      })
    );
  }

  onClickTemplate() {
    this.router.navigate(['/template']);
  }
}
