import { IPageService } from '../Page/Page';
import { IPageContentCell, IPageContent, IPageContentCellIndex } from '../Page/Content';
import { IGridCellDetails } from './GridCell';
import { IGridSelection } from './Grid';

export interface IGridPageContentCell extends IPageContentCell {
  index: IPageContentCellIndex;
}
export interface IGridPageContent extends IPageContent {
  [index: number]: IGridPageContentCell[];
}

export interface IGridPageCellPos {
  x: number;
  y: number;
}
export interface IGridPage {
  uuid: string;
  scale: number;
  content: IGridPageContent;
  originalContent: IGridPageContent;
  deltaContent: IPageContent;
  rows: number;
  cols: number;
  hiddenColumns: number[];
  hiddenRows: number[];
  rowHeights: number[];
  colWidths: number[];
  width: number;
  height: number;
  selectedCols: any[];
  selectedRows: any[];
  selectedCells: any[];
  scrollTop: number;
  scrollbarSize: number;
  scrollbarWidth: number;
  scrollbarHeight: number;
  selection: IGridSelection;
  headings: boolean;
  touch: boolean;
  reset(): void;
  rowPos(row: number): number;
  colPos(row: number): number;
  cellPos(row: number, col: number): IGridPageCellPos;
  resetSelection(): void;
}

class GridPage implements IGridPage {
  // public page!: IPageService;
  public uuid: string = '';
  public scale: number = 1;
  public content: IGridPageContent = [];
  public originalContent: IGridPageContent = [];
  public deltaContent: IPageContent = [];
  public rows: number = 0;
  public cols: number = 0;
  public hiddenColumns: number[] = [];
  public hiddenRows: number[] = [];
  public rowHeights: number[] = [];
  public colWidths: number[] = [];
  public width: number = 0;
  public height: number = 0;
  public selectedCols: any[] = [];
  public selectedRows: any[] = [];
  public selectedCells: any[] = [];
  public scrollTop: number = 0;
  public scrollbarSize: number = 0;
  public scrollbarWidth: number = 0;
  public scrollbarHeight: number = 0;
  public selection: IGridSelection = {
    rowFrom: -1,
    rowTo: -1,
    colFrom: -1,
    colTo: -1,
    heading: false,
    inside: false,
    selected: false,
    keyboard: false,
    type: 'cell'
  };
  public headings: boolean = false;
  public touch: boolean = false;
  public reset(): void {
    this.scale = 0;
    this.content = [];
    this.originalContent = [];
    this.deltaContent = [];
    this.rows = 0;
    this.cols = 0;
    this.hiddenColumns = [];
    this.hiddenRows = [];
    this.rowHeights = [];
    this.colWidths = [];
    this.width = 0;
    this.height = 0;
    this.selectedCells = [];
    this.selectedCols = [];
    this.selectedRows = [];
    this.scrollTop = 0;
    // this.scrollbarWidth = 0;
  }
  public resetSelection(): void {
    this.selectedCells = [];
    this.selectedCols = [];
    this.selectedRows = [];    
    this.selection = {
      rowFrom: -1,
      rowTo: -1,
      colFrom: -1,
      colTo: -1,
      heading: false,
      inside: false,
      selected: false,
      keyboard: false,
      type: 'cell'
    };
  }
  public rowPos(row: number): number {
    return this.rowHeights.slice(0, row).reduce((a: any, b: any) => a + b, 0);
  }
  public colPos(row: number): number {
    return this.colWidths.slice(0, row).reduce((a: any, b: any) => a + b, 0);
  }
  public cellPos(row: number, col: number): IGridPageCellPos {
    return {
      x: this.colWidths.slice(0, col).reduce((a: any, b: any) => a + b, 0),
      y: this.rowHeights.slice(0, row).reduce((a: any, b: any) => a + b, 0)
    };
  }
  public setSelectedCell(cell: IGridCellDetails): void {
    this.selectedCells.push(cell);
  }
}

export default GridPage;
