import axios from 'B/axios';

export enum ContentType {
  EVENTS = 'events',
  TABLE = 'Table',
  LINECHART = 'Linechart',
  BARCHART = 'Barchart',
  LINECHART24 = 'Linechart24',
  BARCHART24 = 'Barchart24',
  THERMOMETER = 'Thermometer',
  TODOS = 'ToDos',
  BALKEN = 'Balken',
  CollectedEvents = 'CollectedEvents',
  INTERVALLAUFGABEN = 'Intervallaufgaben',
  INTERVALLAUFGABEN_ZUWEISEN = 'Intervallaufgaben_zuweisen',
  INFO = 'Info',

}

export enum SpecialType {
  DEFAULT = 0,
  TASKO_NEWS = 3
}

type ConfigFromServer = {
  id: number;
  content_id: number;
  name: string;
  position: string;
  refresh_time: number;
  content_count: number;
  type: ContentType;
  special_type: SpecialType;
};

export type DashSetting = {
  id: number;
  content_id: number;
  name: string;
  position: Position;
  refresh_time: number;
  content_count: number;
  type: ContentType;
  special_type: SpecialType;
};

type Position = {
  x: number;
  y: number;
  height: number;
  width: number;
  z_index: number;
};

export type PointItem = {
  ticket_id: number;
  long_id: string;
  open_tickets?: number;
  name: string;
  current_name: string;
  created_at: Date;
  comment: string;
  color: string;
  group_id: number;
  report: string;
  group_name: string;
  tags?: string[];
  agb_id?: number;
  agb_typ?: string;
};

export type TicketItem = {
  ticket_id: number;
  long_id: string;
  open_tickets?: number;
  name: string;
  current_name: string;
  created_at: Date;
  comment: string;
  color: string;
  group_id: number;
  report: string;
  group_name: string;
  html_info: any;
};

export function withDashToken(url: string): string {
  const token = (window as any).dashboardToken;
  if (!token) return url;
  return url.includes('?')
    ? `${url}&token=${encodeURIComponent(token)}`
    : `${url}?token=${encodeURIComponent(token)}`;
}

function positionEncoder(configs: ConfigFromServer[]): DashSetting[] {
  let newConfigs: DashSetting[] = [];
  for (let c of configs) {
    const position = JSON.parse(c.position);
    const config: DashSetting = { ...c, position };
    newConfigs.push(config);
  }

  return newConfigs;
}

export async function dashBoards() {
  try {
    const res = await axios.get('/dashboards');
    const data: ConfigFromServer[] = res.data;
    return positionEncoder(data);
  } catch (e) {
    return [];
  }
}

export async function dashItemsByGrp(id: number) {
  try {
    const res = await axios.get(withDashToken(`/api/dashboards/grps/${id.toString()}`));
    const data: PointItem[] = res.data;
    return data;
  } catch (e) {
    return [];
  }
}

type position = {
  top: number;
  left: number;
  height: number;
  width: number;
  z_index?: number;
};
export function storePosition(dashID: number, rect: position) {
  let x = rect.left;
  let y = rect.top;
  if (x < 0) {
    x = 0;
  }
  if (y < 0) {
    y = 0;
  }

  const pos = {
    x: x,
    y: y,
    z_index: rect.z_index,
    height: rect.height,
    width: rect.width
  };
  const payload = { dash_id: dashID, dash_pos: pos };

  axios.post('/dashboardpos/save', payload);
}

export type DashContentSetting = {
  id: number;
  name: string;
};
export async function fetchDashContentList(): Promise<DashContentSetting[]> {
  try {
    const { data } = await axios.get('dashcontents');
    return data;
  } catch (e) {
    return [];
  }
}

export async function removeDash(dashID: number) {
  return await axios.delete(`/dashboards/${dashID.toString()}`);
}
