import { resolve } from 'path'
import { readFileSync } from 'fs'
import { PlatformTemplate } from 'types'
import { logError, logInfo } from 'log'

export class PlatformTemplateService {

  static readFromFile(path: string): Promise<PlatformTemplate[]> {
    const fileData = readFileSync(resolve(path), 'utf8')

    if (!fileData && typeof fileData !== 'string') {
      logError({ message: `[DeploymentPlatformClient] Could not find a deployment platform at ${path}` })
      return Promise.reject()
    }

    let potentialTemplates = JSON.parse(fileData)

    if (!Array.isArray(potentialTemplates)) {
      potentialTemplates = [potentialTemplates]
    }

    const templates = potentialTemplates.map(PlatformTemplateService.fakeValidateTemplate)
    logInfo({ message: `[PlatformTemplateService]: Found platforms [${templates.map(x => x.platformName).join(', ')}]` })
    return Promise.resolve(templates)
  }

  static fakeValidateTemplate(obj: any): PlatformTemplate {
    // todo: implement real one
    return obj
  }
}
