import tool from '../utils/tool'
class FloorManager {
    activeContainerId: Number
    floorDatas: any
    constructor() {
        this.activeContainerId = -1
        this.floorDatas = {
            "containers": [],
            "floors": [],
            "pageSet": {}
        }
    }
    
    static getInstance() {
        if (window.top._floorManager == null) {
            window.top._floorManager = new FloorManager();
        }
        return window.top._floorManager;
    }
    fill(data, callback) {
        tool.sendMessage2App({ code: 'fill', data: data }, callback)
    }
    
    renderAllFloors(floorDatas, callback) {
        this.setFloorDatas(floorDatas)
        this.fill(floorDatas, callback)
    }
    
    updateFloor(floorData, callback) {
        if (!floorData) {
            return
        }
        const { containers, floors } = floorData
        const index = this.floorDatas.containers.findIndex((item) => {
            return item.containerId == containers[0].containerId
        })
        if (index != -1) {
            this.floorDatas.containers[index] = containers[0]
            this.floorDatas.floors[index] = floors[0]
        }
        tool.sendMessage2App({ code: 'update', data: floorData }, callback)
    }
    
    addFloor(floorData, preFloorId, callback) {
        if (!floorData) {
            return
        }
        const { containers, floors } = floorData
        const newContainer = containers[0]
        const newFloor = floors[0]
        const index = this.floorDatas.floors.findIndex((floor) => {
            return floor.uid == preFloorId
        })
        if (index == -1) {
            const headerIndex = this.floorDatas.containers.findIndex((item)=>{
                return item.containerPosition == 'header'
            })
            if(headerIndex!=-1){
                this.floorDatas.containers.splice(headerIndex+1, 0, newContainer)
                this.floorDatas.floors.splice(headerIndex+1, 0, newFloor)
            }
            else{
                this.floorDatas.containers.unshift(newContainer)
                this.floorDatas.floors.unshift(newFloor)
            }
        }
        else {
            this.floorDatas.containers.splice(index+1, 0, newContainer)
            this.floorDatas.floors.splice(index+1, 0, newFloor)
        }
        this.fill(this.floorDatas, callback)
    }
    
    deleteFloor(curFloorId, callback) {
        let curFloorIndex = this.floorDatas.containers.findIndex((item) => {
            return item.containerId === curFloorId;
        });
        this.activeContainerId = -1
        this.floorDatas.containers.splice(curFloorIndex, 1);
        this.floorDatas.floors.splice(curFloorIndex, 1);
        let nextIndex = curFloorIndex
        if (nextIndex > this.floorDatas.containers.length - 1) {
            nextIndex = curFloorIndex - 1;
        }
        let nextContainerId = this.floorDatas.containers[nextIndex].containerId;
        this.fill(this.floorDatas, () => {
            if (callback) {
                callback(nextContainerId)
            }
        })
    }
    
    moveFloor(curFloorId, preFloorId, callback) {
        if (curFloorId == undefined || curFloorId == preFloorId) {
            return
        }
        const curIndex = this.floorDatas.containers.findIndex((item) => {
            return item.containerId == curFloorId
        })
        if (curIndex == -1) {
            return
        }
        const curContainer = this.floorDatas.containers[curIndex]
        const curFloor = this.floorDatas.floors[curIndex]
        this.floorDatas.containers.splice(curIndex, 1)
        this.floorDatas.floors.splice(curIndex, 1)
        if (preFloorId != undefined) {
            const preIndex = this.floorDatas.floors.findIndex((floor) => {
                return floor.uid == preFloorId
            })
            if (preIndex == -1) {
                return
            }
            this.floorDatas.containers.splice(preIndex + 1, 0, curContainer)
            this.floorDatas.floors.splice(preIndex + 1, 0, curFloor)
        }
        else {
            this.floorDatas.containers.unshift(curContainer)
            this.floorDatas.floors.unshift(curFloor)
        }
        this.fill(this.floorDatas, ()=>{
            if(callback){
                callback(curFloorId)
            }
        })
    }
    
    getPreContainerId(curContainerId: Number = -1) {
        let result = -1
        if (curContainerId == -1) {
            curContainerId = this.activeContainerId
        }
        const index = this.floorDatas.containers.findIndex((item) => {
            return item.containerId == curContainerId
        })
        if (index > 0) {
            const nextIndex = index - 2
            result = this.floorDatas.containers[nextIndex].containerId
        }
        return result
    }
    
    getNextContainerId(curContainerId: Number = -1) {
        let result = -1
        if (curContainerId == -1) {
            curContainerId = this.activeContainerId
        }
        const index = this.floorDatas.containers.findIndex((item) => {
            return item.containerId == curContainerId
        })
        if (index >= 0 && index < this.floorDatas.containers.length - 1) {
            const nextIndex = index + 1
            result = this.floorDatas.containers[nextIndex].containerId
        }
        return result
    }
    
     getLastContainerId() {
        let result = -1
        const containers = this.floorDatas.containers
        if(containers.length>0){
            result = containers[containers.length-1].containerId
        }
        return result
    }
    
    checkIndex(containerId) {
        let isFirst = false
        let isLast = false
        const {containers} = this.floorDatas
        if(containers && containers.length>0){
            let index = containers.findIndex((item)=>{
                return item.containerId == containerId
            })
            if(containers[0].containerId == 0 && index == 1 || index == 0){
                isFirst = true
            }
            if(index == containers.length - 1){
                isLast = true
            }
        }
        return {isFirst, isLast}
    }
    
    getFloor(containerId): any {
        let result = null
        if (containerId != undefined) {
            result = this.floorDatas.floors.find((floor) => {
                return floor.uid == containerId
            })
        }
        return result
    }
    
    getContainer(uid): any {
        let result = null
        if (uid != undefined) {
            result = this.floorDatas.containers.find((container) => {
                return container.uid == uid
            })
        }
        return result
    }
    
    setFloorDatas(data) {
        this.floorDatas = data
    }
    
    getFloorDatas() {
        return this.floorDatas
    }
    
    setActiveContainerId(id) {
        this.activeContainerId = id
    }
    
    getActiveContainerId(): Number {
        return this.activeContainerId
    }
}
export default FloorManager.getInstance();