import * as url from 'url';

export abstract class UrlConfig {
  constructor(
    protected useSsl: boolean,
    protected domain: string,
    protected port: number,
    protected webSocket: boolean = false
  ) {}

  protected getProtocol(): string {
    return this.webSocket
      ? this.useSsl
        ? 'wss:'
        : 'ws:'
      : this.useSsl
      ? 'https:'
      : 'http:';
  }

  protected getUrl(extra: url.UrlObject): string {
    return url.format({
      ...extra,
      protocol: this.getProtocol(),
      hostname: this.domain,
      port: this.port
    });
  }
}

export class ApiPublicUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get versionInfo2(): string {
    return this.getUrl({
      pathname: '/Pub/verinfo2.aspx'
    });
  }

  public get updateInfo(): string {
    return this.getUrl({
      pathname: '/Pub/updinfo.aspx'
    });
  }
}

export class ApiExternalUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get companyList(): string {
    return this.getUrl({
      pathname: '/Extern/CompanyList.aspx'
    });
  }

  public get urlInfo(): string {
    return this.getUrl({
      pathname: '/Extern/urlinfo.aspx'
    });
  }

  public get checkUserInfoEx(): string {
    return this.getUrl({
      pathname: '/Extern/CheckUserInfoEx.aspx'
    });
  }

  public get tokenUpdate(): string {
    return this.getUrl({
      pathname: '/Extern/TokenUpdate.aspx'
    });
  }
}

export class ApiCommonUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get fileProfileSave(): string {
    return this.getUrl({
      pathname: '/Common/FileProfileSave.aspx'
    });
  }

  public get fileTalkSave(): string {
    return this.getUrl({
      pathname: '/Common/FileTalkSave.aspx'
    });
  }

  public get fileTalkDownload(): string {
    return this.getUrl({
      pathname: '/Common/FileTalkDownload.aspx'
    });
  }

  public get fileTalkShare(): string {
    return this.getUrl({
      pathname: '/Common/FileTalkShare.aspx'
    });
  }

  public get massTalkSave(): string {
    return this.getUrl({
      pathname: '/Common/MassTalkSave.aspx'
    });
  }

  public get massTalkDownload(): string {
    return this.getUrl({
      pathname: '/Common/MassTalkDownload.aspx'
    });
  }

  public get transMassTalkSave(): string {
    return this.getUrl({
      pathname: '/Common/TransMassTalkSave.aspx'
    });
  }

  public get transMassTalkDownload(): string {
    return this.getUrl({
      pathname: '/Common/TransMassTalkDownload.aspx'
    });
  }

  public get translationReq(): string {
    return this.getUrl({
      pathname: '/Common/TranslationReq.aspx'
    });
  }

  public get translationSave(): string {
    return this.getUrl({
      pathname: '/Common/TranslationSave.aspx'
    });
  }
}

export class PiUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get login2(): string {
    return this.getUrl({
      pathname: '/uCapPi/login2'
    });
  }

  public get userTermsAction(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/terms.action'
    });
  }

  public get passwordInitStep1(): string {
    return this.getUrl({
      pathname: '/uCapPi/password/initStep1'
    });
  }

  public get passwordChange(): string {
    return this.getUrl({
      pathname: '/uCapPi/password/change'
    });
  }

  public get policyTotal(): string {
    return this.getUrl({
      pathname: '/uCapPi/policy/total'
    });
  }

  public get policyService(): string {
    return this.getUrl({
      pathname: '/uCapPi/policy/service'
    });
  }

  public get policyPrivacy(): string {
    return this.getUrl({
      pathname: '/uCapPi/policy/privacy'
    });
  }

  public get tokenCheck(): string {
    return this.getUrl({
      pathname: '/uCapPi/tokenCheck'
    });
  }

  public get userMobileAuth(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/mobileAuth'
    });
  }

  public get userUserAuth(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/userAuth'
    });
  }

  public get userCall(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/call'
    });
  }

  public get userMoblieCallAuth(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/moblieCallAuth'
    });
  }

  public get eventSendEventMail(): string {
    return this.getUrl({
      pathname: '/uCapPi/event/sendEventMail'
    });
  }

  public get userMobileInitial1(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/mobileInitial1'
    });
  }

  public get eventSendInvite(): string {
    return this.getUrl({
      pathname: '/uCapPi/event/sendInvite'
    });
  }

  public get userRoom(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/room'
    });
  }

  public get scheduleRetrieveScheduleList(): string {
    return this.getUrl({
      pathname: '/uCapPi/schedule/retrieveScheduleList'
    });
  }

  public get userScreenCapture(): string {
    return this.getUrl({
      pathname: '/uCapPi/user/screenCapture'
    });
  }
}

export class ProtocolUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port, true);
  }

  public get base(): string {
    return this.getUrl({
      pathname: '/'
    });
  }
}

export class VideoConfrenceUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get create(): string {
    return this.getUrl({
      pathname: '/API_CreateConf'
    });
  }

  public get join(): string {
    return this.getUrl({
      pathname: '/AutoLogin.jsp'
    });
  }
}

export class ClickToCallUrlConfig extends UrlConfig {
  constructor(useSsl: boolean, domain: string, port: number) {
    super(useSsl, domain, port);
  }

  public get cdr(): string {
    return this.getUrl({
      pathname: '/clicktocall/api/messenger/cdr'
    });
  }

  public get clicktocall(): string {
    return this.getUrl({
      pathname: '/clicktocall/api/messenger/clicktocall'
    });
  }
}

export interface Environment {
  production: boolean;
  title: string;
  useSsl: boolean;
  webDomain: string;
  webPort: number;
  urls: {
    apiPublic: ApiPublicUrlConfig;
    apiCommon: ApiCommonUrlConfig;
    apiExternal: ApiExternalUrlConfig;
    pi: PiUrlConfig;
    protocol: ProtocolUrlConfig;
    videoConfrence: VideoConfrenceUrlConfig;
    clickToCall: ClickToCallUrlConfig;
  };
  protocol: {
    reconnect: {
      delay: number;
    };
    requestId: {
      min: number;
      max: number;
    };
  };
}

export function build(production: boolean): Environment {
  const devServer = false;
  const useSsl = false;
  const title = 'Woori TALK';

  const webDomain = devServer ? 'bizucap.uplus.co.kr' : '27.122.224.170';
  const webPort = devServer ? 80 : 8011;

  return {
    production,
    title: devServer ? `[개발]${title}` : title,
    useSsl,
    webDomain,
    webPort,
    urls: {
      apiCommon: new ApiCommonUrlConfig(useSsl, webDomain, webPort),
      apiPublic: new ApiPublicUrlConfig(useSsl, webDomain, webPort),
      apiExternal: new ApiExternalUrlConfig(useSsl, webDomain, webPort),
      pi: new PiUrlConfig(useSsl, webDomain, devServer ? 9097 : 9097),
      protocol: new ProtocolUrlConfig(useSsl, webDomain, useSsl ? 9443 : 8080),
      videoConfrence: new VideoConfrenceUrlConfig(
        useSsl,
        devServer ? '101.55.17.24' : '101.55.17.24',
        devServer ? 80 : 80
      ),
      clickToCall: new ClickToCallUrlConfig(
        useSsl,
        devServer ? '169.56.82.147' : '169.56.82.147',
        devServer ? 9095 : 9095
      )
    },
    protocol: {
      reconnect: {
        delay: 1000
      },
      requestId: {
        min: 1,
        max: 59999
      }
    }
  };
}
