import VectorType from '../enum/VectorType.js'
import { floorplanService } from '../Service/FloorplanService.js'
import { elementService } from '../Service/ElementService.js'
import { coordinate } from '../Coordinate.js'
import { draw } from './Draw.js'
import { floorplanData } from '../FloorplanData.js'
import { signService } from '../Service/SignService.js'

export default class Render {
    constructor(layer) {
        this.layer = layer
        this.displayPanos = false
    }

    //绘制户型
    drawGeometry(vector, styleType, flag) {
        if (draw.context == null || vector == null || !vector.geoType) {
            return
        }
        switch (vector.geoType) {
            case VectorType.Wall:
                draw.drawWall(vector, styleType)
                //draw.drawMeasures(vector);
                return
            case VectorType.Point:
                draw.drawPoint(vector)
                return
            case VectorType.Tag:
                draw.drawTag(vector, styleType, flag)
                return
            case VectorType.Rectangle:
                draw.drawRectangle(vector)
                return
            case VectorType.Circle:
                draw.drawCircleGeo(vector)    //drawCircle已经用了
                return
            case VectorType.Icon:
                draw.drawIcon(vector)    
                return
            case VectorType.Arrow:
                draw.drawArrow(vector)    
                return
            case VectorType.Table:
                draw.drawTable(vector)
                return
            case VectorType.Title:
                draw.drawTitle(vector)
                return
            case VectorType.BgImage:
                draw.drawBgImage(vector)
                return
            case VectorType.Compass:
                draw.drawCompass(vector)
                return
            case VectorType.CustomImage:
                draw.drawCustomImage(vector)
                return
        }
        
        if (signService.isSign(vector.geoType)) {
            draw.drawSign(vector)
            return
        }
    }

    //绘制交互的元素
    drawElement(vector) {
        if (draw.context == null) {
            return
        }
        switch (vector.geoType) {
            case VectorType.Point:
                draw.drawCircle(vector)
                break
            case VectorType.Line:
                draw.drawLine(vector)
                break
        }
    }

    redrawElements() {
        if (elementService.vCheckLines.X && elementService.vCheckLines.X.display) {
            this.drawElement(elementService.vCheckLines.X)
        }

        if (elementService.vCheckLines.Y && elementService.vCheckLines.Y.display) {
            this.drawElement(elementService.vCheckLines.Y)
        }

        if (elementService.startAddWall && elementService.startAddWall.display) {
            this.drawElement(elementService.startAddWall)
        }

        if (elementService.newWall && elementService.newWall.display) {
            this.drawElement(elementService.newWall)
        }

        if (elementService.checkLines.X && elementService.checkLines.X.display) {
            this.drawElement(elementService.checkLines.X)
        }

        if (elementService.checkLines.Y && elementService.checkLines.Y.display) {
            this.drawElement(elementService.checkLines.Y)
        }
    }

    redrawRooms(floor) {
        let rooms = roomService.getRooms(floor)
        for (let i = 0; i < rooms.length; ++i) {
            let img = null
            for (let j = 0; j < this.layer.app.CadManager.labels.length; ++j) {
                if (rooms[i].tagName != null && rooms[i].tagName.trim() == this.layer.app.CadManager.labels[j].text) {
                    if (this.layer.app.CadManager.labels[j].type == 'hall') {
                        img = roomService.getHallImg()
                        draw.drawRoomBackGround(rooms[i], img)
                    } else if (this.layer.app.CadManager.labels[j].type == 'room') {
                        img = roomService.getDefaultImg()
                        draw.drawRoomBackGround(rooms[i], img)
                    } else if (this.layer.app.CadManager.labels[j].type == 'other') {
                        img = roomService.getOtherImg()
                        draw.drawRoomBackGround(rooms[i], img)
                    }
                    break
                }
            }

            if (img == null) {
                img = roomService.getOtherImg()
                draw.drawRoomBackGround(rooms[i], img)
            }
        }
    }

    autoRedraw() {
        draw.clear()
        let data = floorplanService.getFloorData()
        if (!data) {
            return
        }

        let title = data.title
        this.drawGeometry(title)

        let bgImage = data.bgImage
        this.drawGeometry(bgImage)

        let compass = data.compass
        this.drawGeometry(compass)

        let walls = data.walls
        for (let key in walls) {
            this.drawGeometry(walls[key])
        }

        let points = data.points
        for (let key in points) {
            this.drawGeometry(points[key])
        }

        //draw.drawSpecialPoint()

        let signs = data.signs
        for (let key in signs) {
            this.drawGeometry(signs[key])
        }

        let tags = data.tags
        for (let key in tags) {
            this.drawGeometry(tags[key])
        }

        let tables = data.tables
        for (let key in tables) {
            this.drawGeometry(tables[key])
        }

        let rectangles = data.rectangles
        for (let key in rectangles) {
            this.drawGeometry(rectangles[key])
        }

        let circles = data.circles
        for (let key in circles) {
            this.drawGeometry(circles[key])
        }

        let arrows = data.arrows
        for (let key in arrows) {
            this.drawGeometry(arrows[key])
        }

        let icons = data.icons
        for (let key in icons) {
            this.drawGeometry(icons[key])
        }

        let customImages = data.customImages
        for (let key in customImages) {
            this.drawGeometry(customImages[key])
        }
        
        draw.drawFrame()
        this.redrawElements()
    }

    autoRedrawForImg() {
        draw.clear()
        draw.drawBackGround()
        let data = floorplanService.getFloorData()
        if (!data) {
            return
        }

        let title = data.title
        this.drawGeometry(title)

        let bgImage = data.bgImage
        this.drawGeometry(bgImage)

        let compass = data.compass
        this.drawGeometry(compass)

        let walls = data.walls
        for (let key in walls) {
            this.drawGeometry(walls[key])
        }

        let points = data.points
        for (let key in points) {
            this.drawGeometry(points[key])
        }

        //draw.drawSpecialPoint()

        let signs = data.signs
        for (let key in signs) {
            this.drawGeometry(signs[key])
        }

        let tags = data.tags
        for (let key in tags) {
            this.drawGeometry(tags[key])
        }

        let tables = data.tables
        for (let key in tables) {
            this.drawGeometry(tables[key])
        }

        let rectangles = data.rectangles
        for (let key in rectangles) {
            this.drawGeometry(rectangles[key])
        }

        let circles = data.circles
        for (let key in circles) {
            this.drawGeometry(circles[key])
        }

        let arrows = data.arrows
        for (let key in arrows) {
            this.drawGeometry(arrows[key])
        }

        let icons = data.icons
        for (let key in icons) {
            this.drawGeometry(icons[key])
        }

        let customImages = data.customImages
        for (let key in customImages) {
            this.drawGeometry(customImages[key])
        }
        
        draw.drawFrame()
        this.redrawElements()
    }

    redrawCore() {
        console.log('重绘！')
        draw.clear()

        let data = floorplanService.getFloorData()
        if (!data) {
            return
        }
        let walls = data.walls
        for (let key in walls) {
            this.drawGeometry(walls[key])
        }

        let points = data.points
        for (let key in points) {
            this.drawGeometry(points[key])
        }

        let signs = data.signs
        for (let key in signs) {
            this.drawGeometry(signs[key])
        }
    }

    clear() {
        draw.clear()
    }

    getContext() {
        return draw.context
    }
}

// const render = new Render()
// export { render }
