// @ts-nocheck
import { SGraphItem } from "@saga-web/graph/lib";
import { SIconTextItem } from '@saga-web/big/lib/items/SIconTextItem';
import { Legend } from '../types/Legend';
import { ItemOrder, SItemStatus } from '@saga-web/big/lib';
import { SFont, SColor, SPainter, SRect } from '@saga-web/draw/lib';
import { SMouseEvent } from "@saga-web/base/lib";
/**
 * 图例节点Item(图标类型)
 *
 * * @author  张宇(taohuzy@163.com)
 */
export class SImageLegendItem extends SIconTextItem {
    /** 图例节点对象数据 */
    data: Legend;
    /** 图标缩放比例 */
    _iconScale: number = 0.6;
    /** 图例数量 */
    _num: number = 1;
    get num(): number {
        return this._num;
    }
    set num(v: number) {
        if (v) {
            this._num = v;
            this.data.Num = v;
        } else {
            this._num = 1;
            this.data.Num = 1;
        }
        this.data.Properties.Num = this._num;
        this.textItem.text = `${this.name}${this.num > 1 ? `×${this.num}` : ''}`;
        this.update();
    }

    get text(): string {
        return this.textItem.text;
    }
    set text(v: string) {
        this.name = v;
        this.data.Name = v;
        this.textItem.text = `${this.name}${this.num > 1 ? `×${this.num}` : ''}`;
        this.update();
    }

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

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      图例节点对象数据
     */
    constructor(parent: SGraphItem | null, data: Legend) {
        super(parent, data.AnchorList);
        this.isTransform = false;
        this.zOrder = ItemOrder.markOrder;
        this.data = data;
        this.id = data.ID;
        this.name = data.Name;
        this.text = data.Name;
        this.moveTo(data.Pos.X, data.Pos.Y);
        if (data.Num) {
            this.num = data.Num;
        }
        if (data.Size) {
            this.width = data.Size.Width * this._iconScale;
            this.height = data.Size.Height * this._iconScale;
        }
        if (data.Properties.Zorder) {
            this.zOrder = data.Properties.Zorder;
        }
        if (data.Properties && data.Properties.Url) {
            this.img.url = data.Properties.Url;
        }
        if (data.Properties && data.Properties.Size && data.Properties.Size.Width) {
            this.sWidth = data.Properties.Size.Width * this._iconScale;
        }
        if (data.Properties && data.Properties.Size && data.Properties.Size.Height) {
            this.sHeight = data.Properties.Size.Height * this._iconScale;
        }
        if (data.Properties.ImgPos) {
            this.img.moveTo(data.Properties.ImgPos.X, data.Properties.ImgPos.Y);
        }
        if (data.Properties && data.Properties.ImgRotate) {
            this.img.rotate = data.Properties.ImgRotate;
        }
        if (data.Properties && data.Properties.font) {
            this.font = new SFont("sans-serif", data.Properties.font);
        }
        if (data.Properties.TextPos) {
            this.textItem.moveTo(data.Properties.TextPos.X, data.Properties.TextPos.Y);
        } else {
            // 偏移二分之一文本高度
            this.textItem.moveTo((this.img.width * 0.5), -(this.font.size * 1.25 * 0.5));
        }
        if (data.Properties && data.Properties.color) {
            this.color = new SColor(data.Properties.color);
        }
        if (data.Properties && data.Properties.IsActive) {
            this.isActive = data.Properties.IsActive;
        }
        if (data.AttachObjectIds && data.AttachObjectIds.length) {
            this.isActive = true;
        }
        if (data.Properties && data.Properties.FrameColor) {
            this.activeColor = new SColor(data.Properties.FrameColor);
        }
    }

    /**
     * 鼠标按下事件
     *
     * @param   event   保存事件参数
     * @return  boolean
     */
    onMouseDown(event: SMouseEvent): boolean {
        if (event.buttons == 1)
        this.$emit("legendItemClick", event);
        return super.onMouseDown(event);
    } // Function onMouseDown()

    toData(): Legend {
        this.data.Pos = { X: this.x, Y: this.y };
        this.data.Size = { Width: this.width / this._iconScale, Height: this.height/ this._iconScale };
        this.data.Name = this.name;
        this.data.Properties.Zorder = this.zOrder;
        this.data.Properties.Url = this.img.url;
        this.data.Properties.TextPos = { X: this.textItem.x, Y: this.textItem.y };
        this.data.Properties.ImgPos = { X: this.img.x, Y: this.img.y };
        this.data.Properties.ImgRotate = this.img.rotate;
        this.data.Properties.Size = {
            Width: this.sWidth / this._iconScale,
            Height: this.sHeight / this._iconScale
        };
        this.data.Properties.font = this.font.size;
        this.data.Properties.color = this.color.value;
        this.data.Properties.Font = this.font.size;
        this.data.Properties.Coler = this.color.value;
        this.data.Properties.FrameColor = this.activeColor.value;
        this.data.Properties.IsActive = this.isActive;
        this.data.AnchorList = this.anchorList.map(t => {
            return {
                ID: t.id,
                Pos: {
                    X: t.x,
                    Y: t.y
                }
            }
        })
        return this.data;
    }

    /**
     * Item绘制框架
     *
     * @param   painter       painter对象
     * @param   rect          绘制区域
     */
    onPaint(painter: SPainter, rect: SRect): void {
        super.onPaint(painter, rect);
        if (this.maskFlag && this.status == SItemStatus.Normal) {
            painter.pen.color = SColor.Transparent;
            painter.brush.color = new SColor("#ffffffa1");
            if (this.selected) {
                painter.drawCircle(this.img.x, this.img.y, (this.sWidth / 2.0) * 1.25);
            } else {
                painter.drawCircle(this.img.x, this.img.y, this.sWidth / 2.0);
            }
        }
    } // Function onPaint()
    
} // Class SImageLegendItem
