import Table from '../Geometry/Table.js'
import Cell from '../Geometry/Cell.js'
import { floorplanService } from './FloorplanService'
import { floorplanData } from '../FloorplanData'
import { mathUtil } from '../MathUtil.js'

export default class TableService {
    constructor() {}

    // 新建cell
    createCell(parent,cellId, floor) {
        let cell = new Cell(parent,cellId)
        floorplanService.addCell(cell, floor)
        return cell
    }

    // 新建table
    createTable(position, tableId, floor) {
        let table = new Table(position, tableId)
        table.setPoints2d()
        floorplanService.addTable(table, floor)
        return table
    }

    setTableInfo(tableInfo) {
        let table = floorplanService.getTable(tableInfo.vectorId)
        table.vectorId = tableInfo.vectorId
        table.center = JSON.parse(JSON.stringify(tableInfo.center))
        table.points = JSON.parse(JSON.stringify(tableInfo.points))
        table.rowLen = tableInfo.rowLen;
        table.colLen = tableInfo.colLen;      
        table.cells = tableInfo.cells;  
    }

    setCellInfo(cellInfo) {
        let cell = floorplanService.getCell(cellInfo.vectorId)
        cell.vectorId = cellInfo.vectorId
        cell.width = cellInfo.width;
        cell.height = cellInfo.height; 
        cell.value = cellInfo.value;
        cell.colIndex = cellInfo.colIndex;
        cell.rowIndex = cellInfo.rowIndex;
        cell.parent = cellInfo.parent;     
    }

    deleteTable(tableId, floorNum) {
        floorplanService.deleteTable(tableId, floorNum)
    }

    deleteCell(cellId, floorNum) {
        floorplanService.deleteCell(cellId, floorNum)
    }

    clearDefaultTables() {
        for (let i = 0; i < floorplanData.floors.length; ++i) {
            let tables = floorplanData.floors[i].tables
            for (let key in tables) {
                let table = tables[key]
                if ((table.value == null || table.value.trim() == '')) {
                    this.deleteTable(table.vectorId, i)
                }
            }
        }
    }
}

const tableService = new TableService()
export { tableService }
