import { floorplanService } from "../Service/FloorplanService";
import { elementService } from "../Service/ElementService";
import { wallService } from "../Service/WallService";
import { historyUtil } from "./HistoryUtil";
import HistoryEvents from "../enum/HistoryEvents";
import { coordinate } from "../Coordinate";

export default class Change {
  constructor() {
    this.lastData = {}; // 每次都是当前数据和lastData进行比较，一般在mouseDown的时候存储进来
    this.elements = {}; // 当前的变化
  }

  // 保存当前记录
  saveCurrentInfo() {
    //this.lastData.subFloorNum = floorplanService.getSubFloor();
    this.lastData.currentFloor = floorplanService.getCurrentFloor();
    this.lastData.points = JSON.parse(
      JSON.stringify(floorplanService.getPoints())
    );
    this.lastData.walls = JSON.parse(
      JSON.stringify(floorplanService.getWalls())
    );

    this.lastData.signs = JSON.parse(
      JSON.stringify(floorplanService.getSigns())
    );

    this.lastData.tags = JSON.parse(JSON.stringify(floorplanService.getTags()));
    this.lastData.cells = JSON.parse(
      JSON.stringify(floorplanService.getCells())
    );
    this.lastData.tables = JSON.parse(
      JSON.stringify(floorplanService.getTables())
    );

    this.lastData.rectangles = JSON.parse(
      JSON.stringify(floorplanService.getRectangles())
    );
    this.lastData.circles = JSON.parse(
      JSON.stringify(floorplanService.getCircles())
    );
    this.lastData.arrows = JSON.parse(
      JSON.stringify(floorplanService.getArrows())
    );
    this.lastData.icons = JSON.parse(
      JSON.stringify(floorplanService.getIcons())
    );

    this.lastData.title = JSON.parse(
      JSON.stringify(floorplanService.getTitle())
    );
    
    let bgImage = floorplanService.getBgImage()
    if(bgImage){
      this.lastData.bgImage = JSON.parse(
        JSON.stringify(bgImage)
      );
    }

    this.lastData.compass = JSON.parse(
      JSON.stringify(floorplanService.getCompass())
    );
    this.lastData.customImages = JSON.parse(
      JSON.stringify(floorplanService.getCustomImages())
    );
  }

  operate() {
    //
    this.elements = {};
    // let flag = this.compareAngle()
    // if (!flag) {
    this.comparePoints();
    this.compareWalls();

    this.compareSigns();

    this.compareTags();
    this.compareCells();
    this.compareTables();

    this.compareRectangles();
    this.compareCircles();
    this.compareArrows();
    this.compareIcons();

    this.compareTitle();
    this.compareBgImage();
    this.compareCompass();
    this.compareCustomImages();
    // }
    // //旋转了
    // else {
    //     this.lastData = {}
    //     return true
    // }
    if (
      this.elements.points.length == 0 &&
      this.elements.walls.length == 0 &&
      this.elements.signs.length == 0 &&
      this.elements.tags.length == 0 &&
      this.elements.cells.length == 0 &&
      this.elements.tables.length == 0 &&
      this.elements.rectangles.length == 0 &&
      this.elements.circles.length == 0 &&
      this.elements.arrows.length == 0 &&
      this.elements.icons.length == 0 &&
      this.elements.title == null &&
      this.elements.bgImage == null &&
      this.elements.compass == null && 
      this.elements.customImages.length == 0
    ) {
      this.saveCurrentInfo();
      return false;
    }

    this.lastData = {};
    // 这里不能取this.records.length-1，因为可能撤销后操作，这时候应该是覆盖，而不是往后面添加
    return true;
  }

  comparePoints() {
    //const currentFloor = floorplanService.getCurrentFloorNum();
    const points = floorplanService.getPoints();
    this.elements.points = [];

    for (const key in points) {
      const point = points[key];
      // 不存在意味着增加
      if (!this.lastData.points[key]) {
        const item = {
          handle: HistoryEvents.AddPoint,
          point: historyUtil.getDataForPoint(point),
        };
        this.elements.points.push(item);
      } else {
        const lastPoint = this.lastData.points[key];
        if (
          point.x == lastPoint.x &&
          point.y == lastPoint.y &&
          JSON.stringify(point.parent) == JSON.stringify(lastPoint.parent)
        ) {
          delete this.lastData.points[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyPoint,
            prePoint: historyUtil.getDataForPoint(lastPoint),
            curPoint: historyUtil.getDataForPoint(point),
          };
          this.elements.points.push(item);
        }
      }
      delete this.lastData.points[key];
    }

    for (const key in this.lastData.points) {
      const item = {
        handle: HistoryEvents.DeletePoint,
        point: historyUtil.getDataForPoint(this.lastData.points[key]),
      };
      this.elements.points.push(item);
    }
  }

  compareWalls() {
    //const currentFloor = floorplanService.getCurrentFloorNum();
    this.elements.walls = [];
    const walls = floorplanService.getWalls();
    for (const key in walls) {
      const wall = walls[key];
      const lastWall = this.lastData.walls[key];

      // 不存在意味着增加
      if (!lastWall) {
        const item = {
          handle: HistoryEvents.AddWall,
          wall: historyUtil.getDataForWall(wall),
        };
        this.elements.walls.push(item);
      } else {
        if (!historyUtil.isDifferentForWalls(wall, lastWall)) {
          delete this.lastData.walls[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyWall,
            preWall: historyUtil.getDataForWall(lastWall),
            curWall: historyUtil.getDataForWall(wall),
          };
          this.elements.walls.push(item);
        }
      }
      delete this.lastData.walls[key];
    }

    for (const key in this.lastData.walls) {
      const item = {
        handle: HistoryEvents.DeleteWall,
        wall: historyUtil.getDataForWall(this.lastData.walls[key]),
      };
      this.elements.walls.push(item);
    }
  }

  compareTags() {
    this.elements.tags = [];
    const tags = floorplanService.getTags();

    for (const key in tags) {
      const tag = tags[key];
      const lastTag = this.lastData.tags[key];

      // 不存在意味着增加
      if (!lastTag) {
        const item = {
          handle: HistoryEvents.AddTag,
          tag: historyUtil.getDataForTag(tag),
        };
        this.elements.tags.push(item);
      } else {
        if (!historyUtil.isDifferentForTags(tag, lastTag)) {
          delete this.lastData.tags[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyTag,
            preTag: historyUtil.getDataForTag(lastTag),
            curTag: historyUtil.getDataForTag(tag),
          };
          this.elements.tags.push(item);
        }
      }
      delete this.lastData.tags[key];
    }

    for (const key in this.lastData.tags) {
      const item = {
        handle: HistoryEvents.DeleteTag,
        tag: historyUtil.getDataForTag(this.lastData.tags[key]),
      };
      this.elements.tags.push(item);
    }
  }

  compareCells() {
    this.elements.cells = [];
    const cells = floorplanService.getCells();

    for (const key in cells) {
      const cell = cells[key];
      const lastCell = this.lastData.cells[key];
      // 不存在意味着增加
      if (!lastCell) {
        const item = {
          handle: HistoryEvents.AddCell,
          cell: historyUtil.getDataForCell(cell),
        };
        this.elements.cells.push(item);
      } else {
        if (!historyUtil.isDifferentForCells(cell, lastCell)) {
          delete this.lastData.cells[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyCell,
            preCell: historyUtil.getDataForCell(lastCell),
            curCell: historyUtil.getDataForCell(cell),
          };
          this.elements.cells.push(item);
        }
      }
      delete this.lastData.cells[key];
    }

    for (const key in this.lastData.cells) {
      const item = {
        handle: HistoryEvents.DeleteCell,
        cell: historyUtil.getDataForCell(this.lastData.cells[key]),
      };
      this.elements.cells.push(item);
    }
  }

  compareTables() {
    this.elements.tables = [];
    const tables = floorplanService.getTables();

    for (const key in tables) {
      const table = tables[key];
      const lastTable = this.lastData.tables[key];

      // 不存在意味着增加
      if (!lastTable) {
        const item = {
          handle: HistoryEvents.AddTable,
          table: historyUtil.getDataForTable(table),
        };
        this.elements.tables.push(item);
      } else {
        if (!historyUtil.isDifferentForTables(table, lastTable)) {
          delete this.lastData.tables[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyTable,
            preTable: historyUtil.getDataForTable(lastTable),
            curTable: historyUtil.getDataForTable(table),
          };
          this.elements.tables.push(item);
        }
      }
      delete this.lastData.tables[key];
    }

    for (const key in this.lastData.tables) {
      const item = {
        handle: HistoryEvents.DeleteTable,
        table: historyUtil.getDataForTable(this.lastData.tables[key]),
      };
      this.elements.tables.push(item);
    }
  }

  compareRectangles() {
    this.elements.rectangles = [];
    const rectangles = floorplanService.getRectangles();

    for (const key in rectangles) {
      const rectangle = rectangles[key];
      const lastRectangle = this.lastData.rectangles[key];

      // 不存在意味着增加
      if (!lastRectangle) {
        const item = {
          handle: HistoryEvents.AddRectangle,
          rectangle: historyUtil.getDataForRectangle(rectangle),
        };
        this.elements.rectangles.push(item);
      } else {
        if (!historyUtil.isDifferentForRectangles(rectangle, lastRectangle)) {
          delete this.lastData.rectangles[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyRectangle,
            preRectangle: historyUtil.getDataForRectangle(lastRectangle),
            curRectangle: historyUtil.getDataForRectangle(rectangle),
          };
          this.elements.rectangles.push(item);
        }
      }
      delete this.lastData.rectangles[key];
    }

    for (const key in this.lastData.rectangles) {
      const item = {
        handle: HistoryEvents.DeleteRectangle,
        rectangle: historyUtil.getDataForRectangle(
          this.lastData.rectangles[key]
        ),
      };
      this.elements.rectangles.push(item);
    }
  }

  compareCircles() {
    this.elements.circles = [];
    const circles = floorplanService.getCircles();

    for (const key in circles) {
      const circle = circles[key];
      const lastCircle = this.lastData.circles[key];

      // 不存在意味着增加
      if (!lastCircle) {
        const item = {
          handle: HistoryEvents.AddCircle,
          circle: historyUtil.getDataForCircle(circle),
        };
        this.elements.circles.push(item);
      } else {
        if (!historyUtil.isDifferentForCircles(circle, lastCircle)) {
          delete this.lastData.circles[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyCircle,
            preCircle: historyUtil.getDataForCircle(lastCircle),
            curCircle: historyUtil.getDataForCircle(circle),
          };
          this.elements.circles.push(item);
        }
      }
      delete this.lastData.circles[key];
    }

    for (const key in this.lastData.circles) {
      const item = {
        handle: HistoryEvents.DeleteCircle,
        circle: historyUtil.getDataForCircle(this.lastData.circles[key]),
      };
      this.elements.circles.push(item);
    }
  }

  compareArrows() {
    this.elements.arrows = [];
    const arrows = floorplanService.getArrows();

    for (const key in arrows) {
      const arrow = arrows[key];
      const lastArrow = this.lastData.arrows[key];

      // 不存在意味着增加
      if (!lastArrow) {
        const item = {
          handle: HistoryEvents.AddArrow,
          arrow: historyUtil.getDataForArrow(arrow),
        };
        this.elements.arrows.push(item);
      } else {
        if (!historyUtil.isDifferentForArrows(arrow, lastArrow)) {
          delete this.lastData.arrows[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyArrow,
            preArrow: historyUtil.getDataForArrow(lastArrow),
            curArrow: historyUtil.getDataForArrow(arrow),
          };
          this.elements.arrows.push(item);
        }
      }
      delete this.lastData.arrows[key];
    }

    for (const key in this.lastData.arrows) {
      const item = {
        handle: HistoryEvents.DeleteArrow,
        arrow: historyUtil.getDataForArrow(this.lastData.arrows[key]),
      };
      this.elements.arrows.push(item);
    }
  }

  compareIcons() {
    this.elements.icons = [];
    const icons = floorplanService.getIcons();

    for (const key in icons) {
      const icon = icons[key];
      const lastIcon = this.lastData.icons[key];

      // 不存在意味着增加
      if (!lastIcon) {
        const item = {
          handle: HistoryEvents.AddIcon,
          icon: historyUtil.getDataForIcon(icon),
        };
        this.elements.icons.push(item);
      } else {
        if (!historyUtil.isDifferentForIcons(icon, lastIcon)) {
          delete this.lastData.icons[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyIcon,
            preIcon: historyUtil.getDataForIcon(lastIcon),
            curIcon: historyUtil.getDataForIcon(icon),
          };
          this.elements.icons.push(item);
        }
      }
      delete this.lastData.icons[key];
    }

    for (const key in this.lastData.icons) {
      const item = {
        handle: HistoryEvents.DeleteIcon,
        icon: historyUtil.getDataForIcon(this.lastData.icons[key]),
      };
      this.elements.icons.push(item);
    }
  }

  compareSigns() {
    //const currentFloor = floorplanService.getCurrentFloorNum();
    this.elements.signs = [];
    const signs = floorplanService.getSigns();

    for (const key in signs) {
      const sign = signs[key];
      const lastSign = this.lastData.signs[key];

      // 不存在意味着增加
      if (!lastSign) {
        const item = {
          handle: HistoryEvents.AddSign,
          sign: historyUtil.getDataForSign(sign),
        };
        this.elements.signs.push(item);
      } else {
        if (!historyUtil.isDifferentForSigns(sign, lastSign)) {
          delete this.lastData.signs[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifySign,
            preSign: historyUtil.getDataForSign(lastSign),
            curSign: historyUtil.getDataForSign(sign),
          };
          this.elements.signs.push(item);
        }
      }
      delete this.lastData.signs[key];
    }

    for (const key in this.lastData.signs) {
      const item = {
        handle: HistoryEvents.DeleteSign,
        sign: historyUtil.getDataForSign(this.lastData.signs[key]),
      };
      this.elements.signs.push(item);
    }
  }

  compareTitle() {
    this.elements.title = null;
    const title = floorplanService.getTitle();
    const lastTitle = this.lastData.title;

    const flag = historyUtil.isDifferentForTitle(title, lastTitle);
    if (flag) {
      const item = {
        handle: HistoryEvents.ModifyTitle,
        preTitle: historyUtil.getDataForTitle(lastTitle),
        curTitle: historyUtil.getDataForTitle(title),
      };
      this.elements.title = item;
    }
  }

  compareBgImage() {
    this.elements.bgImage = null;
    const bgImage = floorplanService.getBgImage();
    const lastBgImage = this.lastData.bgImage;
    let item = {};
    if ((!lastBgImage||!lastBgImage.url)&&(!bgImage||!bgImage.geoType)) {
      return;
    }
    else if ((!lastBgImage||!lastBgImage.url)&&(bgImage&&bgImage.geoType)) {
      item = {
        handle: HistoryEvents.AddBgImage,
        bgImage: historyUtil.getDataForBgImage(bgImage),
      };
      this.elements.bgImage = item;
    } 
    else if((lastBgImage&&lastBgImage.url)&&(!bgImage||!bgImage.geoType)){
      item = {
        handle: HistoryEvents.DeleteBgImage,
        bgImage: historyUtil.getDataForBgImage(this.lastData.bgImage),
      };
      this.elements.bgImage = item;
    }
    else if(historyUtil.isDifferentForBgImage(bgImage, lastBgImage)){
      item = {
        handle: HistoryEvents.ModifyBgImage,
        preBgImage: historyUtil.getDataForBgImage(lastBgImage),
        curBgImage: historyUtil.getDataForBgImage(bgImage),
      };
      this.elements.bgImage = item;
    }
  }

  compareCompass() {
    this.elements.compass = null;
    const compass = floorplanService.getCompass();
    const lastCompass = this.lastData.compass;

    const flag = historyUtil.isDifferentForCompass(compass, lastCompass);
    if (flag) {
      const item = {
        handle: HistoryEvents.ModifyCompass,
        preCompass: historyUtil.getDataForCompass(lastCompass),
        curCompass: historyUtil.getDataForCompass(compass),
      };
      this.elements.compass = item;
    }
  }

  compareCustomImages(){
    this.elements.customImages = [];
    const customImages = floorplanService.getCustomImages();
    for (const key in customImages) {
      const customImage = customImages[key];
      const lastCustomImage = this.lastData.customImages[key];

      // 不存在意味着增加
      if (!lastCustomImage) {
        const item = {
          handle: HistoryEvents.AddCustomImage,
          customImage: historyUtil.getDataForCustomImage(customImage),
        };
        this.elements.customImages.push(item);
      } else {
        if (!historyUtil.isDifferentForCustomImages(customImage, lastCustomImage)) {
          delete this.lastData.customImages[key];
          continue;
        } else {
          const item = {
            handle: HistoryEvents.ModifyCustomImage,
            preCustomImage: historyUtil.getDataForCustomImage(lastCustomImage),
            curCustomImage: historyUtil.getDataForCustomImage(customImage),
          };
          this.elements.customImages.push(item);
        }
      }
      delete this.lastData.customImages[key];
    }

    for (const key in this.lastData.customImages) {
      const item = {
        handle: HistoryEvents.DeleteCustomImage,
        customImage: historyUtil.getDataForCustomImage(this.lastData.customImages[key]),
      };
      this.elements.customImages.push(item);
    }
  }
}

const change = new Change();
export { change };
