import VectorType from '../enum/VectorType.js'
import Geometry from './Geometry'
import { mathUtil } from '../MathUtil.js'
import { coordinate } from '../Coordinate'
import Constant from '../Constant.js'
import Cell from './Cell'
import { floorplanService } from '../Service/FloorplanService.js'
import { tableService } from '../Service/TableService.js'

//不靠墙
export default class Table extends Geometry {
    constructor(center, vectorId) {
        super()
        this.center = center
        this.rowLen = 1;
        this.colLen = 2;
        this.points = []         
        //this.size = [[]];          //保存全部cell的尺寸，方便查询
        this.cells = [];           //cell的数组，里面存放cell.vectorId
        this.geoType = VectorType.Table
        this.setId(vectorId)
        if(!vectorId){
            this.init();
        }
    }

    //初始化两个cell
    init(){
        let cell1 = tableService.createCell(this.vectorId)
        let cell2 = tableService.createCell(this.vectorId)
        cell1.rowIndex = 0;
        cell1.colIndex = 0;
        cell2.rowIndex = 0;
        cell2.colIndex = 1;

        cell1.width = 90;
        cell2.width = 170

        this.cells[0] = []
        this.cells[0][0] = cell1.vectorId
        this.cells[0][1] = cell2.vectorId
        this.setPoints2d()
    }

    isContain(position) {
        let points = []
        points.push(this.points[0])
        points.push(this.points[1])
        points.push(this.points[2])
        points.push(this.points[3])
        return mathUtil.isPointInPoly(position, points)
    }

    setPoints2d() {
        let rows = [];
        let cols = [];
        let height = 0;
        let width = 0;

        for(let i=0;i<this.cells.length;++i){
            for(let j=0;j<this.cells[i].length;++j){
                const cell = floorplanService.getCell(this.cells[i][j]);
                if(!rows.includes(cell.rowIndex)){
                    rows.push(cell.rowIndex);
                    height += cell.height
                }
    
                if(!cols.includes(cell.colIndex)){
                    cols.push(cell.colIndex);
                    width += cell.width
                }
            }
        }

        this.points = []

        this.points[0] = {
            x:this.center.x - ((width / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2,
            y:this.center.y + ((height / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        }

        this.points[1] = {
            x:this.center.x + ((width / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2,
            y:this.center.y + ((height / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        }

        this.points[2] = {
            x:this.center.x + ((width / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2,
            y:this.center.y - ((height / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        }

        this.points[3] = {
            x:this.center.x - ((width / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2,
            y:this.center.y - ((height / coordinate.res) * Constant.defaultZoom) / coordinate.zoom / 2
        }
    }

    setValue(value) {
        this.cells = []; 
        let maxCol = 0;
        let maxRow = 0;
        for(let i=0;i<value.length;++i){
            const item = value[i]
            if(!this.cells[item.rowIndex]){
                this.cells[item.rowIndex] = [];
            }
            let cellId = this.cells[item.rowIndex][item.colIndex]
            let cell = floorplanService.getCell(cellId)
            if(!cell){
                cell = tableService.createCell(this.vectorId)
                this.cells[item.rowIndex][item.colIndex] = cell.vectorId
            }
            cell.width = item.width
            cell.height = item.height
            cell.value = item.value
            cell.colIndex = item.colIndex
            cell.rowIndex = item.rowIndex


            if(maxRow < item.rowIndex){
                maxRow = item.rowIndex
            }

            if(maxCol < item.colIndex){
                maxCol = item.colIndex
            }
        }

        ++maxRow;
        ++maxCol;
        let width=0;
        let height=0;

        //删除多余的行
        for(let i=maxRow;i<this.cells.length;++i){
            for(let j=0;j<this.cells[i].length;++j){
                const cell = floorplanService.getCell(this.cells[i][j]);
                if(cell){
                    floorplanService.deleteCell(this.cells[i][j]);
                }
            }
        }
        this.cells.splice(maxRow, this.cells.length-maxRow); 

        //删除多余的列，貌似目前暂时不用
        for(let i=0;i<maxRow;++i){
            for(let j=maxCol;j<this.cells[i].length;++j){
                const cell = floorplanService.getCell(this.cells[i][j]);
                if(cell){
                    floorplanService.deleteCell(this.cells[i][j]);
                }
            }
            this.cells[i].splice(maxCol, this.cells[0].length-maxCol); 
        }

        this.rowLen = this.cells.length;
        this.colLen = this.cells[0].length;

        for(let i=0;i<maxRow;++i){
            const cell = floorplanService.getCell(this.cells[i][0]);
            height += cell.height
        }

        for(let i=0;i<maxCol;++i){
            const cell = floorplanService.getCell(this.cells[0][i]);
            width += cell.width
        }

        this.points[1] = {
            x:this.points[0].x + width/coordinate.res,
            y:this.points[0].y
        }

        this.points[2] = {
            x:this.points[0].x + width/coordinate.res,
            y:this.points[0].y - height/coordinate.res
        }

        this.points[3] = {
            x:this.points[0].x ,
            y:this.points[0].y - height/coordinate.res
        }

        this.center = {
            x:(this.points[0].x + this.points[1].x)/2,
            y:(this.points[0].y + this.points[2].y)/2
        }
    }

    getCellPos(vectorId){
        let height = 0;
        let width = 0;
        const cell = floorplanService.getCell(vectorId)
        for(let i=0;i<cell.rowIndex;++i){
            const _cell = floorplanService.getCell(this.cells[i][0])
            height += _cell.height*coordinate.ratio;
        }
        for(let i=0;i<cell.colIndex;++i){
            const _cell = floorplanService.getCell(this.cells[0][i])
            width += _cell.width*coordinate.ratio;
        }

        return {
            x:width,
            y:height
        }
    }
}
