import { Injectable, Inject } from '@angular/core';

import {
  SessionStorageService,
  LocalStorageService
} from '@ucap-webmessenger/web-storage';
import { LocaleCode } from '@ucap-webmessenger/core';
import {
  LoginInfo,
  KEY_LOGIN_INFO,
  KEY_LOGIN_RES_INFO,
  KEY_VER_INFO,
  KEY_URL_INFO,
  KEY_LOGOUT_INFO,
  LogoutInfo,
  KEY_AUTH_INFO
} from '../types';
import { PasswordUtil } from '@ucap-webmessenger/pi';

import { DaesangCipherService } from '@ucap-webmessenger/daesang';

import { environment } from '../../environments/environment';
import { AppUserInfo, KEY_APP_USER_INFO } from '@app/types/app-user-info.type';
import { Settings } from '@ucap-webmessenger/ui-settings';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';

@Injectable({
  providedIn: 'root'
})
export class AppAuthenticationService {
  showLoader = false;

  constructor(
    private sessionStorageService: SessionStorageService,
    private localStorageService: LocalStorageService,
    @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
    private daesangCipherService: DaesangCipherService
  ) {}

  authenticated(): boolean {
    const loginInfo = this.sessionStorageService.get<LoginInfo>(KEY_LOGIN_INFO);
    return null !== loginInfo && !!loginInfo.loginId;
  }

  async login(loginInfo: LoginInfo, rememberMe: boolean, autoLogin: boolean) {
    loginInfo = { ...loginInfo, localeCode: LocaleCode.Korean };

    const encLoginPw = this.daesangCipherService.encrypt(
      environment.customConfig.pw.userKey,
      loginInfo.loginPw,
      environment.customConfig.pw.isBase64
    );
    // PasswordUtil.encrypt(loginInfo.loginPw)

    this.sessionStorageService.set<LoginInfo>(KEY_LOGIN_INFO, {
      ...loginInfo,
      initPw: loginInfo.loginId === loginInfo.loginPw,
      loginPw: encLoginPw
    });

    let appUserInfo = this.localStorageService.encGet<AppUserInfo>(
      KEY_APP_USER_INFO,
      environment.customConfig.appKey
    );

    if (!appUserInfo) {
      appUserInfo = {
        idleCheckTime: 10,
        zoom: 100,
        settings: {
          ...environment.productConfig.defaultSettings,
          chat: {
            ...environment.productConfig.defaultSettings.chat,
            downloadPath: `${await this.nativeService.getPath(
              'documents'
            )}/Messenger downloads`
          }
        }
      };
    }

    appUserInfo = {
      ...appUserInfo,
      companyGroupType: loginInfo.companyGroupType,
      companyCode: loginInfo.companyCode,
      loginId: loginInfo.loginId,
      loginPw: loginInfo.loginPw
    };

    if (rememberMe || autoLogin) {
      appUserInfo = {
        ...appUserInfo,
        rememberMe
      };
      appUserInfo.settings.general.autoLogin = autoLogin;
    }

    this.localStorageService.encSet<AppUserInfo>(
      KEY_APP_USER_INFO,
      appUserInfo,
      environment.customConfig.appKey
    );

    this.sessionStorageService.remove(KEY_LOGOUT_INFO);
  }

  logout() {
    this.sessionStorageService.remove(KEY_LOGIN_RES_INFO);
    this.sessionStorageService.remove(KEY_VER_INFO);
    this.sessionStorageService.remove(KEY_LOGIN_INFO);
    this.sessionStorageService.remove(KEY_URL_INFO);
    this.sessionStorageService.remove(KEY_AUTH_INFO);

    let appUserInfo = this.localStorageService.encGet<AppUserInfo>(
      KEY_APP_USER_INFO,
      environment.customConfig.appKey
    );

    appUserInfo = {
      ...appUserInfo,
      settings: {
       ...appUserInfo.settings,
       general: {
         ...appUserInfo.settings.general,
         autoLogin: false
       }
      }
    };

    this.localStorageService.encSet<AppUserInfo>(
      KEY_APP_USER_INFO,
      appUserInfo,
      environment.customConfig.appKey
    );
  }
}
