import type * as shxInterfaces from '@smarthome.exchange/interfaces';
import { SimpleLocalMapper, type IIntegrationEntity, type ISimpleLocalSnapshot, type ISimpleLocalSnapshotOptions, type TSimpleLocalRawData, type TSimpleLocalSnapshotSource } from '../../core/index.js';
import type { IMaryttsConfig } from './marytts.types.js';
import { maryttsDefaultCodec, maryttsDefaultHost, maryttsDefaultLanguage, maryttsDefaultName, maryttsDefaultPort, maryttsDefaultVoice, maryttsProfile } from './marytts.types.js';

export class MaryttsMapper {
  public static toSnapshot(optionsArg: Omit<ISimpleLocalSnapshotOptions<IMaryttsConfig>, 'profile'>): ISimpleLocalSnapshot {
    return SimpleLocalMapper.toSnapshot({ ...optionsArg, profile: maryttsProfile });
  }

  public static toSnapshotFromRaw(configArg: IMaryttsConfig, rawDataArg: TSimpleLocalRawData): ISimpleLocalSnapshot {
    return SimpleLocalMapper.toSnapshot({ profile: maryttsProfile, config: configArg, rawData: rawDataArg, online: true, source: 'manual' });
  }

  public static toStatusSnapshot(configArg: IMaryttsConfig, optionsArg: { online: boolean; source: TSimpleLocalSnapshotSource; error?: string }): ISimpleLocalSnapshot {
    const host = configArg.host || maryttsDefaultHost;
    const port = configArg.port || maryttsDefaultPort;
    const protocol = configArg.useTls || configArg.ssl ? 'https' : 'http';
    const language = configArg.language || configArg.locale || maryttsDefaultLanguage;
    const codec = configArg.codec || maryttsDefaultCodec;
    const voice = configArg.voice || maryttsDefaultVoice;

    return SimpleLocalMapper.toSnapshot({
      profile: maryttsProfile,
      config: configArg,
      online: optionsArg.online,
      source: optionsArg.source,
      error: optionsArg.error,
      rawData: {
        device: {
          id: configArg.uniqueId || `${host}:${port}`,
          name: configArg.name || maryttsDefaultName,
          manufacturer: 'MaryTTS',
          model: 'MaryTTS HTTP server',
          host,
          port,
          protocol,
          configurationUrl: `${protocol}://${host}:${port}`,
        },
        entities: [
          {
            id: 'service',
            name: 'MaryTTS Service',
            platform: 'binary_sensor',
            state: optionsArg.online,
            available: true,
            writable: false,
            deviceClass: 'connectivity',
            attributes: { source: optionsArg.source },
          },
          {
            id: 'language',
            name: 'Default Language',
            platform: 'sensor',
            state: language,
            available: optionsArg.online,
            writable: false,
          },
          {
            id: 'codec',
            name: 'Default Codec',
            platform: 'sensor',
            state: codec,
            available: optionsArg.online,
            writable: false,
          },
          {
            id: 'voice',
            name: 'Default Voice',
            platform: 'sensor',
            state: voice,
            available: optionsArg.online,
            writable: false,
          },
        ],
      },
    });
  }

  public static toDevices(snapshotArg: ISimpleLocalSnapshot): shxInterfaces.data.IDeviceDefinition[] {
    return SimpleLocalMapper.toDevices(maryttsProfile, snapshotArg);
  }

  public static toEntities(snapshotArg: ISimpleLocalSnapshot): IIntegrationEntity[] {
    return SimpleLocalMapper.toEntities(maryttsProfile, snapshotArg);
  }

  public static slug(valueArg: unknown): string {
    return SimpleLocalMapper.slug(valueArg);
  }
}
