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

export default class HistoryUtil {
    constructor() {}

    isDifferentForWalls(wall1, wall2) {
        if (wall1.start == wall2.start && wall1.end == wall2.end && wall1.color == wall2.color) {
            return false
        } else {
            return true
        }
    }
  
    isDifferentForTags(tag1, tag2) {
        if (mathUtil.equalPoint(tag1.center, tag2.center) && tag1.value == tag2.value && tag1.color == tag2.color && tag1.fontSize == tag2.fontSize) {
            return false
        } else {
            return true
        }
    }

    isDifferentForTables(table1, table2) {
        if(!mathUtil.equalPoint(table1.center, table2.center)){
            return true;
        }
        else if(table1.rowLen != table2.rowLen || table1.colLen != table2.colLen || table1.cells.length != table2.cells.length){
            return true;
        }
        else{
            for(let i=0;i<table1.cells.length;++i){
                for(let j=0;j<table1.cells[i].length;++j){
                    const cell1 = floorplanService.getCell(table1.cells[i][j]);
                    const cell2 = floorplanService.getCell(table2.cells[i][j]);
                    if(this.isDifferentForCells(cell1,cell2)){
                        return true;
                    }
                }
            }
        }

        return false
    }

    isDifferentForCells(cell1, cell2) {
        if (cell1.value == cell2.value&&cell1.width == cell2.width&&cell1.height == cell2.height&&cell1.colIndex == cell2.colIndex&&cell1.rowIndex == cell2.rowIndex&&cell1.parent == cell2.parent) {
            return false
        } else {
            return true
        }
    }

    isDifferentForRectangles(rectangle1, rectangle2) {
        for(let i=0;i<rectangle1.points.length;++i){
            if(!mathUtil.equalPoint(rectangle1.points[i], rectangle2.points[i]) || rectangle1.color != rectangle2.color){
                return true;
            }
        }
        return false;
    }

    isDifferentForCircles(circle1, circle2) {
        if(!mathUtil.equalPoint(circle1.center, circle2.center) || circle1.color != circle2.color||circle1.radius != circle2.radius){
            return true;
        }
        else {
            for(let i=0;i<circle1.points.length;++i){
                if(!mathUtil.equalPoint(circle1.points[i], circle2.points[i])){
                    return true;
                }
            }
        }
        return false;
    }

    isDifferentForArrows(arrow1, arrow2) {
        if (mathUtil.equalPoint(arrow1.startPoint, arrow2.startPoint) && mathUtil.equalPoint(arrow1.endPoint, arrow2.endPoint) && arrow1.color == arrow2.color) {
            return false
        } else {
            return true
        }
    }

    isDifferentForIcons(icon1, icon2) {
        if(!mathUtil.equalPoint(icon1.center, icon2.center)){
            return true;
        }
        else if(icon1.radius != icon2.radius){
            return true;
        }
        else if(icon1.value != icon2.value){
            return true;
        }
        else if(icon1.angle != icon2.angle){
            return true;
        }
        else {
            for(let i=0;i<icon1.points.length;++i){
                if(!mathUtil.equalPoint(icon1.points[i], icon2.points[i])){
                    return true;
                }
            }
        }
        return false;
    }

    isDifferentForSigns(sign1, sign2) {
        if (sign1.scale == sign2.scale && JSON.stringify(sign1.center) == JSON.stringify(sign2.center) && sign1.angle == sign2.angle) {
            return false
        } else {
            return true
        }
    }

    isDifferentForTitle(title1, title2) {
        if (title1.value == title2.value) {
            return false
        } else {
            return true
        }
    }

    isDifferentForBgImage(bgImage1, bgImage2) {
        if (bgImage1.url == bgImage2.url && bgImage1.scale == bgImage2.scale && JSON.stringify(bgImage1.center) == JSON.stringify(bgImage2.center)) {
            return false
        } else {
            return true
        }
    }

    isDifferentForCompass(compass1, compass2) {
        if (compass1.angle == compass2.angle) {
            return false
        } else {
            return true
        }
    }

    isDifferentForCustomImages(customImage1, customImage2) {
        if (customImage1.angle == customImage2.angle && customImage1.scale == customImage2.scale && customImage1.url == customImage2.url&& customImage1.ratio == customImage2.ratio) {
            return false
        } else {
            return true
        }
    }

    // wall2赋值给wall1
    assignWallFromWall(wall1, wall2) {
        const wallInfo = {}
        wallInfo.vectorId = wall1.vectorId
        wallInfo.start = wall2.start
        wallInfo.end = wall2.end
        wallInfo.color = wall2.color
        wallService.setWallInfo(wallInfo)
    }

    assignPointFromPoint(point1, point2) {
        const pointInfo = {}
        pointInfo.vectorId = point1.vectorId
        pointInfo.position = { x: point2.x, y: point2.y }
        pointInfo.parent = JSON.parse(JSON.stringify(point2.parent))
        wallService.setPointInfo(pointInfo)
    }

    assignTagFromTag(tag1, tag2) {
        const tagInfo = {}
        tagInfo.vectorId = tag1.vectorId
        tagInfo.value = tag2.value
        tagInfo.color = tag2.color
        tagInfo.fontSize = tag2.fontSize
        tagInfo.center = JSON.parse(JSON.stringify(tag2.center))
        tagInfo.points2d = JSON.parse(JSON.stringify(tag2.points))
        tagService.setTagInfo(tagInfo)
    }

    assignCellFromCell(cell1, cell2) {
        const cellInfo = {}
        cellInfo.vectorId = cell1.vectorId
        cellInfo.width = cell2.width
        cellInfo.height = cell2.height
        cellInfo.value = cell2.value
        cellInfo.colIndex = cell2.colIndex
        cellInfo.rowIndex = cell2.rowIndex
        cellInfo.parent = cell2.parent
        tableService.setCellInfo(cellInfo)
    }

    assignTableFromTable(table1, table2) {
        const tableInfo = {}
        tableInfo.vectorId = table1.vectorId
        tableInfo.rowLen = table2.rowLen
        tableInfo.colLen = table2.colLen
        tableInfo.center = JSON.parse(JSON.stringify(table2.center))
        tableInfo.points = JSON.parse(JSON.stringify(table2.points))
        tableInfo.cells = table2.cells
        tableService.setTableInfo(tableInfo)
    }

    assignRectangleFromRectangle(rectangle1, rectangle2) {
        const rectangleInfo = {}
        rectangleInfo.vectorId = rectangle1.vectorId
        rectangleInfo.angle = rectangle2.angle
        rectangleInfo.color = rectangle2.color
        rectangleInfo.points = JSON.parse(JSON.stringify(rectangle2.points))
        rectangleService.setRectangleInfo(rectangleInfo)
    }

    assignCircleFromCircle(circle1, circle2) {
        const circleInfo = {}
        circleInfo.vectorId = circle1.vectorId
        circleInfo.radius = circle2.radius
        circleInfo.color = circle2.color
        circleInfo.center = JSON.parse(JSON.stringify(circle2.center))
        circleInfo.points = JSON.parse(JSON.stringify(circle2.points))
        circleService.setCircleInfo(circleInfo)
    }

    assignArrowFromArrow(arrow1, arrow2) {
        const arrowInfo = {}
        arrowInfo.vectorId = arrow1.vectorId
        arrowInfo.color = arrow2.color
        arrowInfo.startPoint = JSON.parse(JSON.stringify(arrow2.startPoint))
        arrowInfo.endPoint = JSON.parse(JSON.stringify(arrow2.endPoint))
        arrowService.setArrowInfo(arrowInfo)
    }

    assignIconFromIcon(icon1, icon2) {
        const iconInfo = {}
        iconInfo.vectorId = icon1.vectorId
        iconInfo.radius = icon2.radius
        iconInfo.value = icon2.value
        iconInfo.angle = icon2.angle
        iconInfo.center = JSON.parse(JSON.stringify(icon2.center))
        iconInfo.points = JSON.parse(JSON.stringify(icon2.points))
        iconService.setIconInfo(iconInfo)
    }


    assignSignFromSign(sign1, sign2) {
        const SignInfo = {}
        SignInfo.vectorId = sign1.vectorId
        SignInfo.angle = sign2.angle
        SignInfo.center = JSON.parse(JSON.stringify(sign2.center))
        SignInfo.scale = sign2.scale
        signService.setSignInfo(SignInfo)
    }

    assignTitleFromTitle(title1, title2) {
        const titleInfo = {}
        titleInfo.vectorId = title1.vectorId
        titleInfo.value = title2.value
        floorplanService.updateTitle(titleInfo.value)
    }

    assignBgImageFromBgImage(image1, image2) {
        const bgImageInfo = {}
        bgImageInfo.vectorId = image1.vectorId
        bgImageInfo.url = image2.url
        bgImageInfo.center = JSON.parse(JSON.stringify(image2.center))
        bgImageInfo.scale = image2.scale
        bgImageService.setBgImageInfo(bgImageInfo)
    }

    assignCompassFromCompass(compass1, compass2) {
        const compassInfo = {}
        compassInfo.vectorId = compass1.vectorId
        compassInfo.angle = compass2.angle
        floorplanService.updateCompass(compassInfo.angle )
    }

    assignCustomImageFromCustomImage(customImage1, customImage2) {
        const customImageInfo = {}
        customImageInfo.vectorId = customImage1.vectorId
        customImageInfo.angle = customImage2.angle
        customImageInfo.url = customImage2.url
        customImageInfo.center = JSON.parse(JSON.stringify(customImage2.center))
        customImageInfo.scale = customImage2.scale
        customImageService.setCustomImageInfo(customImageInfo)
    }

    deletePoint(pointId) {
        const point = floorplanService.getPoint(pointId)
        const parent = point.parent
        for (const key in parent) {
            floorplanService.deletePoint(pointId, key)
        }
    }
    getDataForPoint(point) {
        const data = {}
        data.id = point.vectorId
        mathUtil.clonePoint(data, point)
        data.parent = JSON.parse(JSON.stringify(point.parent))
        data.type = point.geoType
        return data
    }
    getDataForWall(wall) {
        const data = {}
        data.id = wall.vectorId
        data.color = wall.color
        data.start = wall.start
        data.end = wall.end
        data.type = wall.geoType
        return data
    }

    getDataForSign(sign) {
        const data = {}
        data.id = sign.vectorId
        data.type = sign.geoType
        data.center = JSON.parse(JSON.stringify(sign.center))
        data.scale = sign.scale
        data.angle = sign.angle
        return data
    }

    getDataForTag(tag) {
        const data = {}
        data.id = tag.vectorId
        data.type = tag.geoType
        data.color = tag.color
        data.fontSize = tag.fontSize
        data.center = {}
        mathUtil.clonePoint(data.center, tag.center)
        data.points = [].concat(tag.points2d)
        data.value = tag.value
        return data
    }

    getDataForCell(cell) {
        const data = {}
        data.id = cell.vectorId
        data.type = cell.geoType
        data.width = cell.width
        data.height = cell.height
        data.value = cell.value
        data.colIndex = cell.colIndex
        data.rowIndex = cell.rowIndex
        data.parent = cell.parent
        return data
    }

    getDataForTable(table) {
        const data = {}
        data.id = table.vectorId
        data.type = table.geoType
        data.rowLen = table.rowLen
        data.colLen = table.colLen
        data.center = {}
        mathUtil.clonePoint(data.center, table.center)
        data.points = [].concat(table.points)
        data.cells = [].concat(table.cells)
        return data
    }

    getDataForRectangle(rectangle) {
        const data = {}
        data.id = rectangle.vectorId
        data.type = rectangle.geoType
        data.color = rectangle.color
        data.angle = rectangle.angle
        data.points = [].concat(rectangle.points)
        return data
    }

    getDataForCircle(circle) {
        const data = {}
        data.id = circle.vectorId
        data.type = circle.geoType
        data.color = circle.color
        data.center = {}
        mathUtil.clonePoint(data.center, circle.center)
        data.points = [].concat(circle.points)
        data.radius = circle.radius
        return data
    }

    getDataForArrow(arrow) {
        const data = {}
        data.id = arrow.vectorId
        data.type = arrow.geoType
        data.color = arrow.color
        data.startPoint = {}
        mathUtil.clonePoint(data.startPoint, arrow.startPoint)
        data.endPoint = {}
        mathUtil.clonePoint(data.endPoint, arrow.endPoint)
        return data
    }

    getDataForIcon(icon) {
        const data = {}
        data.id = icon.vectorId
        data.type = icon.geoType
        data.value = icon.value
        data.angle = icon.angle
        data.center = {}
        mathUtil.clonePoint(data.center, icon.center)
        data.points = [].concat(icon.points)
        data.radius = icon.radius
        return data
    }

    getDataForTitle(title) {
        const data = {}
        data.id = title.vectorId
        data.type = title.geoType
        data.value = title.value
        return data
    }

    getDataForBgImage(image) {
        const data = {}
        data.id = image.vectorId
        data.type = image.geoType
        data.url = image.url
        data.center = {}
        mathUtil.clonePoint(data.center, image.center)
        data.scale = image.scale
        return data
    }

    getDataForCompass(compass) {
        const data = {}
        data.id = compass.vectorId
        data.type = compass.geoType
        data.angle = compass.angle
        return data
    }

    getDataForCustomImage(customImage) {
        const data = {}
        data.id = customImage.vectorId
        data.type = customImage.geoType
        data.angle = customImage.angle
        data.scale = customImage.scale
        data.url = customImage.url
        data.center = {}
        mathUtil.clonePoint(data.center, customImage.center)
        data.ratio = customImage.ratio
        return data
    }
}

const historyUtil = new HistoryUtil()
export { historyUtil }
