import { TinyMceOptions } from './constants/constants';
import { ViewSlot } from 'aurelia-framework';
import { Language, UIFilesColumnInfo } from './model/common-model';
import { RouteConfig } from 'aurelia-router';
import { CacheLocation } from '@auth0/auth0-spa-js';
import { RawEditorOptions } from 'tinymce';
import { EnumerationTypeCategory } from './utils/enumeration-types-helper';
import { PermissionsConfig } from './vm/security/security-helpers';
import { RemoteFileStorage } from './services/file-storage-service';

export interface AuthConfig {
  clientId: string;
  domain: string;
  originalDomain?: string;
  audience: string;
  callback_uri: string;
  logout_uri: string;
  clientSecret?: string;

  /**
   * The location to use when storing cache data. Valid values are `memory` or `localstorage`.
   * The default setting is `memory`.
   *
   * Read more about [changing storage options in the Auth0 docs](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#change-storage-options)
   */
  cacheLocation?: CacheLocation | string; // Added string typing to remove error in project ts compilation

  // Urls for which we do not need a bearer token
  anonymousUrls?: string[];
}

export interface EnumerationTypeConfig {
  useOrder: boolean;
  listByCategory: boolean;
  isUniqueCodeRequired: boolean;
  completeTranslatedFields: boolean;
  showTag: boolean;
  enumerationTypeCategories?: EnumerationTypeCategory[];
  enumerationTypes?: {};
}

export interface AgGridConfig {
  administratorRoles?: number[];
  allowSharedView: boolean;
  defaultAdjustColumns: boolean;
  hasHyperlinkColumn: boolean;
  hyperlinkInNewTab: boolean;
  repeatViewVisibilityInListItem: boolean;
  separateViewsByVisibility: boolean;
  showSystemViewOnlyAsPublicView: boolean;
  showInFloatingBox: boolean;
  showFixedFilter: boolean;
  showFilterBox: boolean;
  showParametersViewBox: boolean;
  showUsedViewInList: boolean;
  showViewBox: boolean;
  showEmptyFieldsCheckbox: boolean;
  stringSeparatorAgGridFilter?: string;
  suppressMenu: boolean;
  sortColumnByHeaderName: boolean;
}

export interface ContainerConfig {
  AlertContainer?: Element;
  ToastContainer?: Element;
  DialogContainer?: ViewSlot & { anchor?: Element };
  TaskbarContainer?: ViewSlot & { anchor?: Element };
  FloatingContainer?: Element;
}

interface B2CConfig {
  clientId: string;
  redirectUri: string;
  names: {
    signUpSignIn: string;
  };
  authorities: {
    signin: string;
  };
  authorityDomain: string;
  scopes: Array<string>;
}

export interface PermissionSetting {
  action?: string | string[];
  resource?: string;
}

export interface RouteSettings extends PermissionSetting {
  group?: string;
  subGroup?: string;
  icon?: string;
  fontIconsClass?: string;
  isDefault?: boolean;
  parentName?: string;
  classes?: string;
  /**
   * If the ui-tab-router-view is used, set tab to false to not tabulate this route.
   *
   * @type {boolean}
   */
  tab?: boolean;
  permissionSettings?: Array<PermissionSetting>;
  params?: { [key: string]: any };
}

export interface ExtendedRouteConfig extends RouteConfig {
  settings?: RouteSettings;
}

export interface GlobalConfig {
  applicationTitle?: string;
  remoteServiceName?: string;
  remoteFileStorageServiceConfig?: RemoteFileStorage.IServiceConfig;
  // remoteFileController?: string;
  remoteTinyMceController?: string;
  breezeControllerUri?: string;
  applicationSettingsTypeName?: string;
  resourceNames?: {};
  configureMetadataStore?: (metaDatastore, manager) => any;
  googleApiKey?: string;
  isAutomaticSmallDevice?: boolean;
  showPaginationBoundaryLinks?: boolean;
  defaultLocale: string;
  configuredLanguages?: Language[];
  uiBagdeDisplay: 'top' | 'bottom';
  showHistory: boolean;
  showChat: boolean;
  redirectionIfNotAuthorized: boolean;
  displayWeekPicker: boolean;
  defaultUIFilesColumnsInfo: Array<UIFilesColumnInfo>;
  keyboardShortcutFwBox: boolean;
  uiSelectFocusSearchWhenOpen: boolean;
  uiInputInputMode: string;
}

export interface MenuConfig {
  primaryColor: string;
  maxLevelVerticalNavbar: number;
}

export interface CommunicationConfig {
  showHistoryChatInButton: boolean;
  /**
   * Value of 0 or less will disable auto close
   */
  uINotificationAlertAutoCloseTimeInMilliseconds: number;
  allowChatCountFetch: boolean;
}



export abstract class Config {
  routes: ExtendedRouteConfig[];
  routesPrefix?: string;
  authConfig?: AuthConfig;
  b2cConfig?: B2CConfig;
  permissionsConfig?: PermissionsConfig;

  agGridConfig: AgGridConfig = {
    allowSharedView: false,
    defaultAdjustColumns: true,
    hasHyperlinkColumn: true,
    hyperlinkInNewTab: false,
    repeatViewVisibilityInListItem: true,
    separateViewsByVisibility: false,
    showSystemViewOnlyAsPublicView: true,
    showInFloatingBox: false,
    showFixedFilter: false,
    showFilterBox: false,
    showParametersViewBox: false,
    showUsedViewInList: true,
    showViewBox: false,
    showEmptyFieldsCheckbox: false,
    stringSeparatorAgGridFilter: '&&',
    suppressMenu: true,
    sortColumnByHeaderName: true
  };

  globalConfig: GlobalConfig = {
    isAutomaticSmallDevice: true,
    showPaginationBoundaryLinks: true,
    defaultLocale: 'fr-BE',
    uiBagdeDisplay: 'top',
    showHistory: true,
    showChat: true,
    redirectionIfNotAuthorized: false,
    displayWeekPicker: false,
    remoteTinyMceController: '/api/tiny-mce/',
    defaultUIFilesColumnsInfo: [
      {
        columnName: "description",
        columnVisibility: true,
        width: 2
      },
      {
        columnName: "fileTypeId",
        columnVisibility: true,
        width: 2
      },
      {
        columnName: "createdTime",
        columnVisibility: false,
        width: 1
      }
    ],
    keyboardShortcutFwBox: false,
    remoteFileStorageServiceConfig: {
      controller: "/api/file/",
      secure: false,
      canDownloadFiles: true
    },
    uiSelectFocusSearchWhenOpen: true,
    uiInputInputMode: ''
  }

  defaultRichTextEditorOptions: RawEditorOptions = {
    height: 400,
    menubar: false,
    content_style: TinyMceOptions.contentStyle,
    toolbar: TinyMceOptions.toolbar,
    powerpaste_allow_local_images: true,
    plugins: TinyMceOptions.plugins,
    automatic_uploads: true,
    file_picker_types: 'image',
    contextmenu: TinyMceOptions.contextMenu
  }

  enumerationTypeConfig: EnumerationTypeConfig = {
    useOrder: false,
    listByCategory: true,
    isUniqueCodeRequired: false,
    completeTranslatedFields: false,
    showTag: false,
  };

  menuConfig: MenuConfig = {
    primaryColor: '#000000',
    maxLevelVerticalNavbar: 3,
  };

  containerConfig: ContainerConfig = {};

  communicationConfig: CommunicationConfig = {
    showHistoryChatInButton: false,
    uINotificationAlertAutoCloseTimeInMilliseconds: 0,
    allowChatCountFetch: true,
  };

  constructor(environment?: { remoteApiUrl?: string; auth?: AuthConfig }) {
    if (environment == null) {
      return;
    }
    if (environment.remoteApiUrl) {
      this.globalConfig.remoteServiceName = environment.remoteApiUrl;
    }
    if (environment.auth) {
      this.authConfig = {
        clientId: environment.auth.clientId,
        domain: environment.auth.domain,
        originalDomain: environment.auth.originalDomain,
        audience: environment.auth.audience,
        callback_uri: environment.auth.callback_uri,
        logout_uri: environment.auth.logout_uri,
        clientSecret: environment.auth.clientSecret,
        cacheLocation: environment.auth.cacheLocation,
      };
    }
  }

}
