import { floorplanService } from './Service/FloorplanService.js'
import { wallService } from './Service/WallService.js'
import { tagService } from './Service/TagService'
import { signService } from './Service/SignService'
import { rectangleService } from './Service/RectangleService.js'
import { circleService } from './Service/CircleService.js'
import { arrowService } from './Service/ArrowService.js'
import { iconService } from './Service/IconService.js'
import { tableService } from './Service/TableService.js'
import { customImageService } from './Service/CustomImageService.js'
import { bgImageService } from './Service/BgImageService.js'

export default class Load {
    constructor(layer) {
        this.layer = layer
        this.version = 'v1.1'
        this.vectorsJson = null
    }

    async load(floorsData) {
        if(!floorsData||!floorsData.floors){
            floorplanService.initFloor(0)
            //添加标题
            const title = floorplanService.createTitle()
            floorplanService.addTitle(title)
            //添加指南针
            const compass = floorplanService.createCompass()
            floorplanService.addCompass(compass)
            // //添加背景图片
            // const bgImage = floorplanService.createBgImage()
            // floorplanService.addBgImage(bgImage)
            // if(bgImage.url){
            //     const imageData = await floorplanService.loadImageData(bgImage.url)
            //     bgImage.setImageData(imageData)
            // }
            return;
        }
        floorplanService.setCurrentId(floorsData.currentId)
        for (let i = 0; i < floorsData.floors.length; ++i) {
            let floor = floorsData.floors[i]
            floorplanService.initFloor(i)
            for (let key in floor.points) {
                wallService.createPoint(floor.points[key].x, floor.points[key].y, floor.points[key].vectorId, i)
            }

            for (let key in floor.walls) {
                let wall = wallService.createWall(floor.walls[key].start, floor.walls[key].end, floor.walls[key].vectorId, i)
                floor.walls[key].color && wall.setColor(floor.walls[key].color)
            }

            for (let key in floor.tags) {
                let tag = tagService.createTag(floor.tags[key].center, floor.tags[key].vectorId, i)
                tag.setValue(floor.tags[key].value)
                floor.tags[key].color && tag.setColor(floor.tags[key].color)
                floor.tags[key].fontSize && tag.setFontSize(floor.tags[key].fontSize)
            }

            for (let key in floor.customImages) {
                let customImage = await customImageService.createCustomImage(floor.customImages[key].url,floor.customImages[key].center, floor.customImages[key].vectorId)
                customImage.setAngle(floor.customImages[key].angle)
                customImage.setScale(floor.customImages[key].scale)
                customImage.setRatio(floor.customImages[key].ratio)
            }

            for (let key in floor.tables) {
                let table = tableService.createTable(floor.tables[key].center, floor.tables[key].vectorId, i)
                table.rowLen = floor.tables[key].rowLen
                table.colLen = floor.tables[key].colLen
                table.points = floor.tables[key].points
                table.cells = floor.tables[key].cells
            }

            for (let key in floor.cells) {
                let cell = tableService.createCell(floor.cells[key].parent,floor.cells[key].vectorId, i)
                cell.width = floor.cells[key].width
                cell.height = floor.cells[key].height
                cell.value = floor.cells[key].value
                cell.colIndex = floor.cells[key].colIndex
                cell.rowIndex = floor.cells[key].rowIndex
            }

            for (let key in floor.rectangles) {
                let rectangle = rectangleService.createRectangle(floor.rectangles[key].points[0], floor.rectangles[key].points[2], floor.rectangles[key].vectorId,i)
                floor.rectangles[key].color && rectangle.setColor(floor.rectangles[key].color)
            }

            for (let key in floor.circles) {
                let circle =  circleService.createCircle2(floor.circles[key].center, floor.circles[key].radius, floor.circles[key].vectorId,i)
                floor.circles[key].color && circle.setColor(floor.circles[key].color)
            }

            for (let key in floor.arrows) {
                let arrow = arrowService.createArrow(floor.arrows[key].startPoint,floor.arrows[key].endPoint,floor.arrows[key].vectorId,i)
                floor.arrows[key].color && arrow.setColor(floor.arrows[key].color)
            }

            for (let key in floor.icons) {
                iconService.createIcon2(floor.icons[key].center,floor.icons[key].radius,floor.icons[key].value,floor.icons[key].vectorId,i)
            }

            for (let key in floor.signs) {
                let sign = signService.createSign(floor.signs[key].center, floor.signs[key].geoType, floor.signs[key].vectorId,i)
                sign.angle = floor.signs[key].angle
                floor.signs[key].scale && sign.setScale(floor.signs[key].scale)
            }

            const title = floorplanService.createTitle(floor.title.value, floor.title.vectorId, i)
            floorplanService.addTitle(title)

            const compass = floorplanService.createCompass(floor.compass.angle, floor.compass.vectorId, i)
            floorplanService.addCompass(compass)
            
            if (floor.bgImage && floor.bgImage.url) {
                await bgImageService.createBgImage(floor.bgImage.url)
            }
        }
    }

}
