import config from '@/application/config'
import type { AssetDataT } from '@/application/types'
import Dexie from 'dexie'

export class Assets {
  private db: Dexie

  constructor() {
    this.db = new Dexie('assets')
    this.db.version(1).stores({
      assets: 'key, group'
    })
  }

  async download(asset: AssetDataT) {
    try {
      // Check if the asset already exists, then check if updatedAt is newer
      const _asset = await this.db.table('assets').get(asset.key)
      if (_asset && _asset.updatedAt > asset.updatedAt) {
        return
      }

      // Download the asset
      const response = await fetch(config.server_endpoint + asset.data)
      const blob = await response.blob()

      // Store the asset in the database
      await this.db.table('assets').put({
        key: asset.key,
        data: blob,
        group: asset.group,
        updatedAt: asset.updatedAt,
        originX: asset.originX,
        originY: asset.originY,
        isAnimated: asset.isAnimated,
        frameRate: asset.frameRate,
        frameWidth: asset.frameWidth,
        frameHeight: asset.frameHeight,
        frameCount: asset.frameCount
      })
    } catch (error) {
      console.error(`Failed to add asset ${asset.key}:`, error)
    }
  }

  async get(key: string) {
    try {
      const asset = await this.db.table('assets').get(key)
      if (asset) {
        return {
          ...asset,
          data: URL.createObjectURL(asset.data) // Convert blob to data URL
        }
      }
    } catch (error) {
      console.error(`Failed to retrieve asset ${key}:`, error)
    }
    return null
  }

  async getByGroup(group: string) {
    try {
      const assets = await this.db.table('assets').where('group').equals(group).toArray()
      return assets.map((asset) => ({
        ...asset,
        data: URL.createObjectURL(asset.data) // Convert blob to data URL
      }))
    } catch (error) {
      console.error(`Failed to retrieve assets for group ${group}:`, error)
      return []
    }
  }

  async delete(key: string) {
    try {
      await this.db.table('assets').delete(key)
    } catch (error) {
      console.error(`Failed to delete asset ${key}:`, error)
    }
  }
}