import { getRepository } from 'typeorm'

import { User } from '@things-factory/auth-base'
import { Board } from '@things-factory/board-service'
import { Setting } from '@things-factory/setting-base'
import { Domain } from '@things-factory/shell'

import {
  CARTON_LABEL_SETTING_KEY,
  CARTON_LABEL_BY_SKU_SETTING_KEY,
  HOME_DASHBOARD_PREFIX,
  LOCATION_LABEL_SETTING_KEY,
  PALLET_LABEL_SETTING_KEY,
  ITEM_LABEL_SETTING_KEY,
  PRODUCT_LABEL_SETTING_KEY,
  REUSABLE_PALLET_LABEL_SETTING_KEY,
  INTENALIZED_AWB_SETTING_KEY,
  ROUTE_LABEL_SETTING_KEY,
  TOTE_LABEL_SETTING_KEY
} from '../../../opa-app-setting-constants'

export const boardSettingsResolver = {
  async boardSettings(_: any, params: any, context: any) {
    const { domain }: { domain: Domain } = context.state

    /***********************************************************************************
     * Temporary solution for dashboard user type
     ***********************************************************************************/
    const user: User = await getRepository(User)
      .createQueryBuilder('USER')
      .leftJoinAndSelect('USER.roles', 'ROLES')
      .leftJoinAndSelect('ROLES.domain', 'ROLES_DOMAIN')
      .where('USER.id = :userId', { userId: context.state.user.id })
      .andWhere('ROLES_DOMAIN.id = :domainId', { domainId: domain.id })
      .getOne()

    const userType = user.roles.filter(role => role.description)[0].description
    /**********************************************************************************/

    const HOME_BOARD = `${HOME_DASHBOARD_PREFIX}:${userType}`
    // const HOME_BOARD = `${HOME_DASHBOARD_PREFIX}:${context.state.user.userType}`

    const queryBuilder = getRepository(Setting).createQueryBuilder()
    const names = [
      LOCATION_LABEL_SETTING_KEY,
      PRODUCT_LABEL_SETTING_KEY,
      PALLET_LABEL_SETTING_KEY,
      ITEM_LABEL_SETTING_KEY,
      REUSABLE_PALLET_LABEL_SETTING_KEY,
      HOME_BOARD,
      CARTON_LABEL_SETTING_KEY,
      CARTON_LABEL_BY_SKU_SETTING_KEY,
      INTENALIZED_AWB_SETTING_KEY,
      ROUTE_LABEL_SETTING_KEY,
      TOTE_LABEL_SETTING_KEY
    ]

    var qb = queryBuilder
      // .innerJoin(Board, 'Board', 'Board.id = CAST(Setting.value AS uuid)')
      .innerJoin(Board, 'Board', 'Setting.value = CAST(Board.id AS char(36))')
      // .innerJoin(Board, 'Board', 'Board.id = Setting.value')
      .select([
        'Setting.id as id',
        'Setting.name as name',
        'Setting.value as value',
        'Board.id as boardId',
        'Board.name as boardName',
        'Board.description as boardDescription',
        'Board.thumbnail as boardThumbnail'
      ])
      .where('Setting.domain_id = :domain', { domain: context.state.domain.id })

    if (names && names.length) qb.andWhere('Setting.name IN (:...names)', { names })

    var boardSettingList = await qb.getRawMany()

    return boardSettingList.map(boardSetting => {
      var setting: any = {
        board: {}
      }

      for (let key in boardSetting) {
        if (key.includes('board')) {
          let originKey = key
          key = key.replace('board', '').toLowerCase()
          setting.board[key] = boardSetting[originKey]
        } else {
          setting[key] = boardSetting[key]
        }
      }

      if (setting.name.indexOf(HOME_DASHBOARD_PREFIX) == 0) {
        setting.name = HOME_DASHBOARD_PREFIX
      }

      return setting
    })
  }
}
