import { Filters } from './filter.types';
export enum DataFrom {
  DEFAULT,
  DATA_CONTAINER,
}

export interface ComponentItem {
  id: number;
  uniqueTag: string;
  name: string;
  config: ComponentConfig;
  base: {
    module_name: string;
    version: string;
  };
  autoUpdate: {
    isAuto: boolean;
    interval: number;
  };
  // dataConfig: null | ComponentDataConfig;
  dataConfigs: ComponentDataConfigs;
  dataContainers: Array<{ id: number; enable: boolean; rank: number }>;
  dataType: DataType;
  dataFrom: DataFrom;
  actions: ActionConfig;
  events: EventConfig[];
  baseURL?: string;
  filters: Filters;
  from: ComponentType;
  parent: null | number;
  staticData: {
    data: null | any[];
    field: null | ComponentDataField;
  };
  triggers: EventTriggerConfig;
  useFilter: boolean;
  errorMessage?: null | string;
  screenId: number;
}

type EventTriggerConfig = {
  name: string;
  value: string;
}[];

export enum ComponentType {
  OFFICIAL,
  TENANT,
}

type ComponentDataField = {
  name: string;
  value: string;
  desc: string;
}[];
type ComponentConfig = any[];

export type DataType =
  | 'static'
  | 'mysql'
  | 'mssql'
  | 'oracle'
  | 'csv'
  | 'api'
  | 'apiGateway'
  | 'postgresql'
  | 'clickhouse'
  | 'dtInsight'
  | 'ws'
  | 'db2'
  | 'damengdb'
  | 'mqtt'
  | 'dtableapi';

export type Method = 'get' | 'post' | 'put' | 'delete' | 'patch';

interface SQLDataConfig {
  data: {
    data_id: string;
    sql: string;
  };
}

export interface ComponentsById {
  [x: number]: ComponentItem;
}

interface APIDataConfig {
  data: {
    data_id: string;
    path: string;
    params: string;
    method: Method;
    body: string;
    headers: string;
    needCookie: boolean;
    reqFromBack: boolean;
  };
}

interface ComponentDataConfig {
  csv?: {
    data: {
      data_id: string;
    };
  };
  api?: APIDataConfig;
  dtInsight?: APIDataConfig;
  apiGateway?: APIDataConfig;
  mysql?: SQLDataConfig;
  oracle?: SQLDataConfig;
  mssql?: SQLDataConfig;
  postgresql?: SQLDataConfig;
}

export interface DataTableConfig {
  data_id: string;
  table_id: string;
  view_id: string;
  max_records: number;
  method: string;
}

export type ComponentDataConfigItem = {
  data: any;
  fields: ComponentDataField;
  config?: DataTableConfig | APIDataConfig['data'] | SQLDataConfig['data'] | { data_id: string };
  callbackKeys?: string[];
};

export type ComponentDataConfigs = {
  [x in DataType]: ComponentDataConfigItem;
};

type AnimationType = 'opacity' | 'slide' | 'zoom' | 'fade';

type ComponentScope = 'current' | 'global';

type ConditionType = 'all' | 'none';

type CompareType = '==' | '!=' | '<' | '>' | '<=' | '>=' | 'include' | 'exclude';

interface ConditionConfig {
  id: string;
  name: string;
  type: 'field' | 'custom';
  field: string;
  expected: string;
  compore: CompareType;
  code: string;
}

export type EventConfig = EventConfigOld | EventConfigNew;

export interface EventConfigOld {
  id: string;
  name: string;
  trigger: string;
  conditionType: ConditionType;
  conditions: ConditionConfig[];
  reverse?: boolean;
  action: string;
  actionData: Object;
  animation: {
    type: AnimationType;
    timingFunction: TimingFunction;
    duration: number;
    delay: number;
  };
  component: string[];
  componentConfig: Object;
  componentScope: ComponentScope;
  scale: {
    x: number;
    y: number;
    origin: string;
    lock: boolean;
  };
  stateId: string;
  translate: {
    toX: number;
    toY: number;
  };
  unmount: boolean;
}

export interface EventConfigNew {
  id: string;
  name: string;
  trigger: string;
  conditionType: ConditionType;
  conditions: ConditionConfig[];
  reverse: boolean;
  actions: {
    action: string;
    actionData: Object;
    animation: {
      type: AnimationType;
      timingFunction: TimingFunction;
      duration: number;
      delay: number;
    };
    component: string[];
    componentConfig: Object;
    componentScope: ComponentScope;
    scale: {
      x: number;
      y: number;
      origin: string;
      lock: boolean;
    };
    stateId: string;
    translate: {
      toX: number;
      toY: number;
    };
    unmount: boolean;
  }[];
}

type ActionConfig = {
  name: string;
  value: string;
  config: ComponentConfig;
}[];
