
import { SRect, SSize, SPoint } from "@sybotan-web/base";
import SGraphyPolygonItem from './newItems/SGraphyPolygonItem.js'
import { SGraphyImgItem } from './newItems/SGraphyImgItem.js'
import { SGraphyScene, SMouseEvent } from "@sybotan-web/graphy";
import { SPen, SPainter, SColor } from "@sybotan-web/draw";
import { dataItemPath, dataItem, dataSpaceItem, dataInterface, PolygonItemInterface, ImgItemInterface } from './dataType'   //传入参数的参数接口类型
import Axios from 'axios';
import pako from "./until/pako.js"
import tools from "./until/tool.js"
/**
 * @name mainScene
 * @time 2019-07.07;
 * 添加所有item的场景到scene中
 */

export default class mainScene extends SGraphyScene {

    data: dataInterface | null;

    private _isShowSpace: boolean = true;  // 是否显示空间；
    private _isShowWallList: boolean = true;  //是否展示墙体；
    private _isShowEquipmentList: boolean = true; //是否展示设备；
    private _isShowVrtualWallList: boolean = true; //是否展示虚拟墙

    // 设置是否显示空间
    get isShowSpace(): boolean {
        return this._isShowSpace
    }
    set isShowSpace(b: boolean) {
        this._isShowSpace = b
    }

    // 设置是否显示墙
    get isShowWallList(): boolean {
        return this._isShowWallList
    }
    set isShowWallList(b: boolean) {
        this._isShowWallList = b
    }

    // 设置是否显示设备
    get isShowEquipmentList(): boolean {
        return this._isShowEquipmentList
    }
    set isShowEquipmentList(b: boolean) {
        this._isShowEquipmentList = b
    }

    // 设置是否显示虚拟墙
    get isShowVrtualWallList(): boolean {
        return this._isShowVrtualWallList
    }
    set isShowVrtualWallList(b: boolean) {
        this._isShowVrtualWallList = b
    }

    /**
     * @param data 绘制空间地图得所有参数
     */

    constructor(data: dataInterface | null) {
        super()
        this.data = data;
        if (this.data != null) {
            this.addAllItemList(this.data);
        }
    }
    //获取参数
    urlGetData(url: string) {
        let that = this;
        return new Promise((relove, reject) => {
            Axios({
                method: 'get',
                url: url,
                data: {},
                responseType: 'blob',
                // contentType: "charset=utf-8"
            }).then(res => {
                var blob = res.data;
                this.zipToJson(blob).then((jsonData: any) => {
                    that.addAllItemList(jsonData)
                    relove()
                }).catch((error: any) => {
                    console.log(error)
                });
                // console.log(reader)
            }).catch(res => {
                console.log(res)
            })
        })

    }
    // 压缩包变为json格式数据
    zipToJson(blob: any): any {
        let data = null;
        var reader = new FileReader();
        reader.readAsBinaryString(blob);
        let _this = this;
        return new Promise((resolve) => {
            reader.onload = function (readerEvt: any) {
                var binaryString = readerEvt.target.result;
                //解压数据
                let base64Data = btoa(binaryString)
                let unGzipData = pako.unzip(base64Data)
                data = unGzipData;
                if (data.WallList && data.WallList.length) {
                    tools.changeMap(data.WallList, -1, "PointList");
                }
                if (data.SpaceList && data.SpaceList.length) {
                    tools.changeMap(data.SpaceList, -1, "Paths");
                }
                if (data.ColumnList && data.ColumnList.length) {
                    tools.changeMap(data.ColumnList, -1, "Path");
                }
                if (data.VirtualWallList && data.VirtualWallList.length) {
                    tools.changeMap(data.VirtualWallList, -1, "PointList");
                }
                if (data.EquipmentList && data.EquipmentList.length) {
                    tools.changeMap(data.EquipmentList, -1, "PointList");
                }
                resolve(data)
            };
        })
    }
    // 以下是绘制方法

    /** 
     * 增添所有绘制item(地图);
    */
    addAllItemList(data: dataInterface) {
        let space: dataSpaceItem[] = data.SpaceList;
        // let wall:
        this.addSpaceList(space) //绘制空间
        this.addWallList()       //绘制墙
        let images: ImgItemInterface[] = data.images
        this.addImageList(images)
    }

    /**
     * 添加所有空间到scene 中
     * @param space 空间list
     */
    addSpaceList(space: dataSpaceItem[]): void {
        if (space && space.length) {
            for (let i = 0; i < space.length; i++) {
                if (space[i].Paths[1] && space[i].Paths[1].length >= 2) {
                    this.addItem(this.constructeItem(
                        {
                            parent: null,
                            space: space[i],
                            businessType: 'space'
                        }));
                }
            }
            for (let i = 0; i < space.length; i++) {
                if (space[i].Paths[0] && space[i].Paths[0].length >= 2 && !space[i].Paths[1]) {
                    this.addItem(this.constructeItem(
                        {
                            parent: null,
                            space: space[i],
                            businessType: 'space'
                        }));
                }
            }
        }
    }
    // 绘制墙体
    addWallList(): void {

    }
    // 绘制设备
    addEquipmentList() {
    }
    // 绘制柱体
    addColumnListList(): void {
    }
    // 绘制虚拟墙
    addVrtualWallList(): void {
    }
    // 绘制图片
    addImageList(imageList: ImgItemInterface[]): void {
        if (imageList && imageList.length) {
            imageList.map(t => {
                this.addItem(new SGraphyImgItem(null, t.img, t.X, t.Y, t.width, t.height))
            })
        }
    }
    /**
     * 产生item实例
     */
    constructeItem(PolygonItemInterfaceData: PolygonItemInterface): SGraphyPolygonItem {
        return new SGraphyPolygonItem(PolygonItemInterfaceData)
    }
    // 鼠标交互类事件

    // 点击
    click(_this: any, fn: any): void {
        this.root.children.forEach(item => {
            item.connect("click", _this, fn);
        });
    }
    // 双击
    dbclick(_this: any, fn: any): void {
        this.root.children.forEach(item => {
            item.connect("doubleClick", _this, fn);
        });
    }
    // 按下
    mouseDown(_this: any, fn: any) {
        this.root.children.forEach(item => {
            item.connect("mouseDown", _this, fn);
        });
    }
    //移动
    mouseMove(_this: any, fn: any) {
        this.root.children.forEach(item => {
            item.connect("mouseMove", _this, fn);
        });
    }
    // 点解结束
    mouseUp(_this: any, fn: any) {
        this.root.children.forEach(item => {
            item.connect("mouseUp", _this, fn);
        });
    }

} 