import { existsSync, readFileSync } from "fs"
import path from "path"
import * as Log from "@saeeol/core/util/log"

const log = Log.create({ service: "saeeol-logo" })

const CUSTOM_ART_FILE = "ascii-art.txt"

const saeeol = [
  `.######.  ....  ...##...  ....  ########  ....  ########  ....  .######.  ....  ##......`,
  `##....##  ....  ..####..  ....  ##......  ....  ##......  ....  ##....##  ....  ##......`,
  `##......  ....  .##..##.  ....  ##......  ....  ##......  ....  ##....##  ....  ##......`,
  `.#####..  ....  ##....##  ....  ######..  ....  ######..  ....  ##....##  ....  ##......`,
  `......##  ....  ########  ....  ##......  ....  ##......  ....  ##....##  ....  ##......`,
  `##....##  ....  ##....##  ....  ##......  ....  ##......  ....  ##....##  ....  ##......`,
  `.######.  ....  ##....##  ....  ########  ....  ########  ....  .######.  ....  ########`,
]

let cachedCustom: string[] | null | undefined

function findProjectDir(): string | undefined {
  try {
    const cwd = process.cwd()
    const home = path.join(
      process.env.XDG_DATA_HOME || path.join(require("os").homedir(), ".local", "share"),
      "saeeol",
    )
    for (const dir of [cwd, home]) {
      const p = path.join(dir, ".saeeol", CUSTOM_ART_FILE)
      if (existsSync(p)) return p
    }
  } catch {}
  return undefined
}

export function loadCustomArt(): string[] | null {
  if (cachedCustom !== undefined) return cachedCustom
  const filePath = findProjectDir()
  if (!filePath) {
    cachedCustom = null
    return null
  }
  try {
    const content = readFileSync(filePath, "utf-8")
    const lines = content.split(/\r?\n/).filter((l) => l.trim().length > 0)
    if (lines.length === 0) {
      cachedCustom = null
      return null
    }
    log.info("loaded custom ASCII art", { path: filePath, lines: lines.length })
    cachedCustom = lines
    return lines
  } catch {
    cachedCustom = null
    return null
  }
}

export function art(): string[] {
  return loadCustomArt() ?? saeeol
}

export function tui() {
  return art()
}

export function plain() {
  return art()
}

const yes = new Set(["1", "true", "yes", "on"])
const no = new Set(["0", "false", "no", "off"])

function flag(value: string | undefined) {
  const key = value?.toLowerCase()
  if (!key) return
  if (yes.has(key)) return true
  if (no.has(key)) return false
}

export function supports(env = process.env, platform = process.platform) {
  const override = flag(env.SAEEOL_UNICODE_LOGO)
  if (override !== undefined) return override
  if (env.TERM === "dumb") return false
  if (env.SSH_TTY) return false
  if (env.SSH_CLIENT) return false
  if (env.SSH_CONNECTION) return false
  if (env.ConEmuPID) return false
  if (env.ANSICON) return false
  return true
}

export function session(
  title: string,
  id: string | undefined,
  dim: string,
  normal: string,
) {
  const lines = art()
  return [
    ``,
    `${lines[0]}`,
    ...lines.slice(1, -1),
    `${lines[lines.length - 1]}${dim}saeeol -s ${id}${normal}`,
  ].join("\n")
}
