import {Base64Decoder} from './converter/base64-decoder';
import {Base64Encoder} from './converter/base64-encoder';
import {BinToDecConverter} from './converter/bin-to-dec-converter';
import {Converter} from './converter/converter';
import {DecToBinConverter} from './converter/dec-to-bin-converter';
import {DecToHexConverter} from './converter/dec-to-hex-converter';
import {HexToDecConverter} from './converter/hex-to-dec-converter';
import {HTMLEntitiesDecoder} from './converter/htmlentities-decoder';
import {HTMLEntitiesEncoder} from './converter/htmlentities-encoder';
import {Injectable} from '@angular/core';
import {NativeLibraryWrapperService} from './native-library-wrapper.service';
import {PunycodeDecoder} from './converter/punycode-decoder';
import {PunycodeEncoder} from './converter/punycode-encoder';
import {QuotedPrintableDecoder} from './converter/quoted-printable-decoder';
import {QuotedPrintableEncoder} from './converter/quoted-printable-encoder';
import {ROT13Converter} from './converter/rot13-converter';
import {URIComponentDecoder} from './converter/uricomponent-decoder';
import {URIComponentEncoder} from './converter/uricomponent-encoder';
import {URIDecoder} from './converter/uridecoder';
import {URIEncoder} from './converter/uriencoder';
import {UTF8Decoder} from './converter/utf8-decoder';
import {UTF8Encoder} from './converter/utf8-encoder';

@Injectable({
  providedIn: 'root'
})
export class ConverterRegistryService {
  private converters: Converter[] = [];

  constructor(private wrapper: NativeLibraryWrapperService) {
    this.init();
  }

  public getAllConverters(): Converter[] {
    return this.converters;
  }

  public getConverter(id: string): Converter {
    return this.converters.find((converter: Converter): boolean => converter.getId() === id);
  }

  private init(): void {
    this.registerConverter(new Base64Encoder());
    this.registerConverter(new Base64Decoder());
    this.registerConverter(new URIEncoder());
    this.registerConverter(new URIDecoder());
    this.registerConverter(new URIComponentEncoder());
    this.registerConverter(new URIComponentDecoder());
    this.registerConverter(new HTMLEntitiesEncoder());
    this.registerConverter(new HTMLEntitiesDecoder());
    this.registerConverter(new QuotedPrintableEncoder(this.wrapper));
    this.registerConverter(new QuotedPrintableDecoder(this.wrapper));
    this.registerConverter(new DecToHexConverter());
    this.registerConverter(new HexToDecConverter());
    this.registerConverter(new DecToBinConverter());
    this.registerConverter(new BinToDecConverter());
    this.registerConverter(new PunycodeEncoder(this.wrapper));
    this.registerConverter(new PunycodeDecoder(this.wrapper));
    this.registerConverter(new UTF8Encoder(this.wrapper));
    this.registerConverter(new UTF8Decoder(this.wrapper));
    this.registerConverter(new ROT13Converter());
  }

  private registerConverter(converter: Converter): void {
    // Don't allow duplicate registration of the same converter id
    if (this.converters.some((c: Converter): boolean => c.getId() === converter.getId())) {
      throw new Error(`Converter-ID ${converter.getId()} is already registered!`);
    }

    this.converters.push(converter);
  }
}