import { floorplanData } from '../FloorplanData'
import { coordinate } from '../Coordinate.js'
import Constant from '../Constant'

import Title from '../Geometry/Title.js'
import BgImage from '../Geometry/BgImage.js'
import Compass from '../Geometry/Compass.js'

export class FloorplanService {
    constructor() {
        this.currentId = 0 // 当前可用id
        this.currentFloor = 0 // 当前楼层，第一层是0
        this.angle = 0 //旋转角度
    }

    setCurrentId(id) {
        this.currentId = id
    }

    getCurrentId() {
        return this.currentId
    }

    updateCurrentId() {
        ++this.currentId
    }

    setCurrentFloor(floor) {
        if (floorplanData.floors.length == 1) {
            this.currentFloor = 0
        } else {
            this.currentFloor = floor
        }
    }

    getCurrentFloor() {
        return this.currentFloor
    }

    getFloorNum() {
        return floorplanData.floors.length
    }

    initFloor(floorNum) {
        floorplanData.initFloor(floorNum)
    }

    getFloors() {
        return floorplanData.floors
    }

    getPoint(pointId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].points[pointId]
    }

    deletePoint(pointId, wallId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let point = this.getPoint(pointId)
        //有可能先删除墙，导致点没了
        if (point) {
            if (Object.keys(point.parent).length == 0) {
                point = null
                delete floorplanData.floors[floor].points[pointId]
            } else if (Object.keys(point.parent).length == 1 && !wallId) {
                delete floorplanData.floors[floor].points[pointId]
            } else if (Object.keys(point.parent).length == 1 && point.parent[wallId]) {
                delete floorplanData.floors[floor].points[pointId]
            } else if (Object.keys(point.parent).length == 1 && !point.parent[wallId]) {
                return
            } else {
                delete point.parent[wallId]
            }
        }
    }

    getWall(wallId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].walls[wallId]
    }

    deleteWall(wallId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let wall = this.getWall(wallId, floor)

        this.deletePoint(wall.start, wallId, floor)
        this.deletePoint(wall.end, wallId, floor)
        delete floorplanData.floors[floor].walls[wallId]
    }

    getAngle() {
        return this.angle
    }

    setAngle(angle) {
        this.angle = angle
    }

    getFloorData(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor]
    }

    getWalls(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].walls
    }

    getPoints(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].points
    }

    addWall(wall, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].walls[wall.vectorId] = wall
    }

    addPoint(point, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].points[point.vectorId] = point
    }

    addRectangle(rectangle,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].rectangles[rectangle.vectorId] = rectangle
    }

    getRectangle(rectangleId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].rectangles[rectangleId]
    }

    deleteRectangle(rectangleId, floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let rectangle = this.getRectangle(rectangleId, floor)
        rectangle = null
        delete floorplanData.floors[floor].rectangles[rectangleId]
    }

    addCircle(circle,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].circles[circle.vectorId] = circle
    }

    getCircle(circleId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].circles[circleId]
    }

    deleteCircle(circleId, floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let circle = this.getCircle(circleId, floor)
        circle = null
        delete floorplanData.floors[floor].circles[circleId]
    }

    addArrow(arrow,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].arrows[arrow.vectorId] = arrow
    }

    getArrow(arrowId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].arrows[arrowId]
    }

    deleteArrow(arrowId, floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let arrow = this.getArrow(arrowId, floor)
        arrow = null
        delete floorplanData.floors[floor].arrows[arrowId]
    }

    addIcon(icon,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].icons[icon.vectorId] = icon
    }

    getIcon(iconId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].icons[iconId]
    }

    deleteIcon(iconId, floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let icon = this.getIcon(iconId, floor)
        icon = null
        delete floorplanData.floors[floor].icons[iconId]
    }

    addSign(sign, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].signs[sign.vectorId] = sign
    }

    getSign(signId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].signs[signId]
    }

    deleteSign(signId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let sign = this.getSign(signId, floor)
        sign = null
        delete floorplanData.floors[floor].signs[signId]
    }

    addTag(tag, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].tags[tag.vectorId] = tag
    }

    getTag(tagId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].tags[tagId]
    }

    deleteTag(tagId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let tag = this.getTag(tagId, floor)
        tag = null
        delete floorplanData.floors[floor].tags[tagId]
    }

    getTags(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].tags
    }

    addTable(table, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].tables[table.vectorId] = table
    }

    getTable(tableId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].tables[tableId]
    }

    deleteTable(tableId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let table = this.getTable(tableId, floor)
        table = null
        delete floorplanData.floors[floor].tables[tableId]
    }

    getTables(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].tables
    }

    addCell(cell, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].cells[cell.vectorId] = cell
    }

    getCell(cellId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].cells[cellId]
    }

    deleteCell(cellId, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        let cell = this.getCell(cellId, floor)
        cell = null
        delete floorplanData.floors[floor].cells[cellId]
    }

    getCells(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].cells
    }

    getRectangles(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].rectangles
    }

    getCircles(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].circles
    }

    getArrows(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].arrows
    }

    getIcons(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].icons
    }
    getSigns(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].signs
    }

    clear() {
        if (floorplanData.floors[this.currentFloor]) {
            floorplanData.floors[this.currentFloor].points = {}
            floorplanData.floors[this.currentFloor].walls = {}
            floorplanData.floors[this.currentFloor].rectangles = {}
            floorplanData.floors[this.currentFloor].circles = {}
            floorplanData.floors[this.currentFloor].tags = {}
            floorplanData.floors[this.currentFloor].tables = {}
            floorplanData.floors[this.currentFloor].cells = {}
            floorplanData.floors[this.currentFloor].signs = {}
            floorplanData.floors[this.currentFloor].arrows = {}
            floorplanData.floors[this.currentFloor].icons = []
        }
    }

    deleteFloorData() {
        floorplanData.floors = []
    }

    createTitle(value,vectorId,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        const title = new Title(value,vectorId,floor)
        return title
    }

    addTitle(title, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].title = title
    }

    updateTitle(value,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        const title = floorplanData.floors[floor].title
        title.setValue(value)
    }

    getTitle(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].title
    }

    createBgImage(value,vectorId,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        const image = new BgImage(value,vectorId,floor)
        return image
    }

    addBgImage(image, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].image = image
    }

    async updateBgImage(src){
        const floor = this.currentFloor
        const img = floorplanData.floors[floor].image
        img.setSrc(src)
        if(img.src){
            const imageData = await floorplanService.loadImageData(img.src)
            img.setImageData(imageData)
        }
      
    }

    getBgImage(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].image
    }

    createCompass(value,vectorId,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        const compass = new Compass(value,vectorId,floor)
        return compass
    }

    addCompass(compass, floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        floorplanData.floors[floor].compass = compass
    }

    updateCompass(value,floor){
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        const compass = floorplanData.floors[floor].compass
        compass.setAngle(value)
    }

    // eslint-disable-next-line no-dupe-class-members
    getCompass(floor) {
        if (floor == null || typeof floor == 'undefined') {
            floor = this.currentFloor
        }
        return floorplanData.floors[floor].compass
    }

    async loadImageData(src){
        const imageData = await new Promise((resolve, reject) => {
            var img = new Image()
            img.src = src;
            img.crossOrigin=""
            img.onload = function () {
                resolve(img)
            }.bind(this)
        })
        return imageData
    }
}

const floorplanService = new FloorplanService()
export { floorplanService }
