// @ts-nocheck
import { SGraphItem, SLineStyle } from "@saga-web/graph/lib";
import { SPoint, SPainter, SColor, SLineCapStyle } from "@saga-web/draw/lib";
import { ItemOrder, SLineItem, SItemStatus } from '@saga-web/big/lib';
import { Marker } from '../types/Marker';

/**
 * 标识对象Item(线类型)
 *
 * * @author  张宇(taohuzy@163.com)
 */
export class SLineMarkerItem extends SLineItem {
    /** 起始锚点  */
    startItem: SGraphItem | null = null;
    /** 结束锚点  */
    endItem: SGraphItem | null = null;

    /** 标识对象数据 */
    data: Marker;

    /** 是否完成绘制  */
    get status(): SItemStatus {
        return this._status;
    }
    set status(v: SItemStatus) {
        this._status = v;
        if (v == SItemStatus.Edit) {
            this.zOrder = ItemOrder.markOrder;
        } else {
            this.zOrder = ItemOrder.lineOrder;
        }
        this.update();
    }

    /** 是否蒙版遮罩  */
    _maskFlag: boolean = false;
    get maskFlag(): boolean {
        return this._maskFlag;
    }
    set maskFlag(v: boolean) {
        if (v === this._maskFlag) {
            return
        }
        this._maskFlag = v;
        this.update()
    }

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      标识对象数据
     */
    constructor(parent: SGraphItem | null, data: Marker) {
        super(parent);
        this.zOrder = ItemOrder.lineOrder;
        this.data = data;
        this.id = data.ID;
        this.name = data.Name;
        this.moveTo(data.Pos.X, data.Pos.Y);
        if (data.Properties.Zorder) {
            this.zOrder = data.Properties.Zorder;
        }
        if (data.Properties && data.Properties.Line) {
            let setPointList: SPoint[];
            setPointList = data.Properties.Line.map(i => {
                return new SPoint(i.X, i.Y)
            })
            this.line = setPointList;
        }
        if (data.Properties && data.Properties.LineWidth) {
            this.lineWidth = data.Properties.LineWidth;
        }
        if (data.Properties && data.Properties.LineStyle) {
            this.lineStyle = data.Properties.LineStyle;
        }
        if (data.Properties && data.Properties.StrokeColor) {
            this.strokeColor = new SColor(data.Properties.StrokeColor);
        }
    } // Constructor

    /** 接收事件作出修改  */
    changePos() {
        if (this.startItem) {
            // 判断删除equip后,不移动
            if (this.startItem.parent) {
                let scenePoint: SPoint = this.startItem.boundingRect().center();
                this.line[0] = this.startItem.mapToScene(scenePoint.x, scenePoint.y);
            }
        }
        if (this.endItem) {
            // 删除equip后
            if (this.endItem.parent) {
                let scenePoint: SPoint = this.endItem.boundingRect().center();
                this.line[1] = this.endItem.mapToScene(scenePoint.x, scenePoint.y);
            }
        }
    }

    toData(): Marker {
        this.data.Pos = { X: this.x, Y: this.y };
        this.data.Properties.Line = this.line.map(pos => {
            return {
                X: pos.x,
                Y: pos.y
            }
        });
        this.data.Properties.Zorder = this.zOrder;
        this.data.Properties.LineWidth = this.lineWidth;
        this.data.Properties.StrokeColor = this.strokeColor.value;
        this.data.Properties.LineStyle = this.lineStyle;
        if (this.startItem && this.startItem.parent) {
            this.data.Properties.StartItemId = this.startItem.id;
        }
        if (this.endItem && this.endItem.parent) {
            this.data.Properties.EndItemId = this.endItem.id;
        }
        return this.data;
    }

    onDraw(painter: SPainter) {
        if (this.maskFlag && this.status == SItemStatus.Normal) {
            let color = new SColor(this.strokeColor);
            color.alpha = 100;
            painter.pen.color = new SColor(this.strokeColor);
            if (this.lineStyle == SLineStyle.Dashed) {
                painter.pen.lineDash = [
                    painter.toPx(this.lineWidth * 3),
                    painter.toPx(this.lineWidth * 7)
                ];
            }
            else if (this.lineStyle == SLineStyle.Dotted) {
                painter.pen.lineDash = [
                    painter.toPx(this.lineWidth),
                    painter.toPx(this.lineWidth)
                ];
            }
            painter.drawLine(this.line[0], this.line[1]);
        } else {
            super.onDraw(painter);
        }
    }
} // Class SLineMarkerItem
