import type { IConfigFlow, IConfigFlowContext, IConfigFlowStep, IDiscoveryCandidate } from '../../core/types.js';
import { androidtvRemoteApiPort, androidtvRemotePairPort } from './androidtv_remote.constants.js';
import type { IAndroidtvRemoteConfig } from './androidtv_remote.types.js';

export class AndroidtvRemoteConfigFlow implements IConfigFlow<IAndroidtvRemoteConfig> {
  public async start(candidateArg: IDiscoveryCandidate, contextArg: IConfigFlowContext): Promise<IConfigFlowStep<IAndroidtvRemoteConfig>> {
    void contextArg;
    return {
      kind: 'form',
      title: 'Connect Android TV Remote',
      description: 'Configure an Android TV Remote protocol v2 host. Pairing and live protocol transport require an injected executor.',
      fields: [
        { name: 'host', label: 'Host', type: 'text', required: true },
        { name: 'apiPort', label: 'API port', type: 'number' },
        { name: 'pairPort', label: 'Pairing port', type: 'number' },
        { name: 'deviceName', label: 'Device name', type: 'text' },
        { name: 'macAddress', label: 'MAC address', type: 'text' },
        { name: 'enableIme', label: 'Enable IME updates', type: 'boolean' },
      ],
      submit: async (valuesArg) => {
        const host = this.stringValue(valuesArg.host) || candidateArg.host;
        if (!host) {
          return { kind: 'error', title: 'Android TV Remote configuration failed', error: 'Host is required.' };
        }
        const apiPort = this.numberValue(valuesArg.apiPort) || this.numberValue(candidateArg.metadata?.apiPort) || candidateArg.port || androidtvRemoteApiPort;
        const pairPort = this.numberValue(valuesArg.pairPort) || this.numberValue(candidateArg.metadata?.pairPort) || androidtvRemotePairPort;
        const deviceName = this.stringValue(valuesArg.deviceName) || candidateArg.name;
        const macAddress = this.stringValue(valuesArg.macAddress) || candidateArg.macAddress || this.stringValue(candidateArg.metadata?.macAddress);
        return {
          kind: 'done',
          title: 'Android TV Remote configured',
          config: {
            host,
            apiPort,
            pairPort,
            deviceName,
            macAddress,
            manufacturer: candidateArg.manufacturer,
            model: candidateArg.model,
            enableIme: typeof valuesArg.enableIme === 'boolean' ? valuesArg.enableIme : true,
            deviceInfo: {
              id: candidateArg.id || macAddress,
              name: deviceName,
              host,
              apiPort,
              pairPort,
              macAddress,
              manufacturer: candidateArg.manufacturer,
              model: candidateArg.model,
            },
          },
        };
      },
    };
  }

  private stringValue(valueArg: unknown): string | undefined {
    return typeof valueArg === 'string' && valueArg.trim() ? valueArg.trim() : undefined;
  }

  private numberValue(valueArg: unknown): number | undefined {
    return typeof valueArg === 'number' && Number.isFinite(valueArg) && valueArg > 0 ? valueArg : undefined;
  }
}
