/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2016-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */
import { SPainter, SRect, SColor, SFont, SPoint, SSize } from "@persagy-web/draw";
import { SGraphItem, STextOrigin, SLineStyle, SAnchorItem } from "@persagy-web/graph/";
import { SItemStatus } from "@persagy-web/big";
import { SGraphEdit } from ".."
/**
 * 文本编辑类
 *
 * @author  韩耀龙(han_yao_long@163.com)
 */
export class STextEdit extends SGraphEdit {
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //属性
    /**编辑状态 */
    protected _status: SItemStatus = SItemStatus.Normal;
    get status(): SItemStatus {
        return this._status;
    }
    set status(value: SItemStatus) {
        const oldStatus = this._status;
        const newStatus = value;
        this._status = value;
        //状态变更触发事件
        this.$emit('StatusChange', oldStatus, newStatus)
        this.update();
    }
    /** 记录painter */
    private _painter: SPainter | null = null;

    /** 文本内容 */
    private _text: string = "";
    get text(): string {
        return this._text;
    }
    set text(v: string) {
        this._text = v;
        this._textArr = this.text.split(/\n/g);
        this.drawFormatText();
        this.update();
    }

    /** 切割后的文本数组 */
    private _textArr: string[] = [];
    /** 锚点list  */
    anchorList: SAnchorItem[] = [];
    /** 宽度  */
    private _width: number = 64;
    get width(): number {
        return this._width;
    }
    set width(v: number) {
        if (v > 0) {
            if (v != this._width) {
                let w = this._width;
                this._width = v;
                this.onResize(
                    new SSize(w, this._height),
                    new SSize(this._width, this._height)
                );
            }
        }
    }

    /** 高度  */
    private _height: number = 64;
    get height(): number {
        return this._height;
    }
    set height(v: number) {
        if (v > 0) {
            if (v != this._height) {
                let h = this._height;
                this._height = v;
                this.onResize(
                    new SSize(this._width, h),
                    new SSize(this._width, this._height)
                );
            }
        }
    }

    /** 原点  */
    origin = new SPoint();

    /**
     * 宽高发发生变化
     *
     * @param   oldSize 改之前的大小
     * @param   newSize 改之后大小
     * */
    protected onResize(oldSize: SSize, newSize: SSize) { } // Function onResize()

    /** 文本颜色 */
    private _color: SColor = new SColor("#333333");
    get color(): SColor {
        return this._color;
    }
    set color(v: SColor) {
        this._color = v;
        this.update();
    }

    /** 字体 */
    private _font: SFont;
    get font(): SFont {
        return this._font;
    }
    set font(v: SFont) {
        this._font = v;
        this.drawFormatText();
        this.update();
    }

    /** 背景色 */
    private _backgroundColor: SColor = SColor.Transparent;
    get backgroundColor(): SColor {
        return this._backgroundColor;
    }
    set backgroundColor(v: SColor) {
        this._backgroundColor = v;
        this.update();
    }

    /** 边框色 */
    private _strokeColor: SColor = SColor.Transparent;
    get strokeColor(): SColor {
        return this._strokeColor;
    }
    set strokeColor(v: SColor) {
        this._strokeColor = v;
        console.log(this._strokeColor)
        this.update();
    }

    /** 边框宽度 */
    private _lineWidth: number = 1;
    get lineWidth(): number {
        return this._lineWidth;
    }
    set lineWidth(v: number) {
        this._lineWidth = v;
        this.update();
    }

    /** 边框样式    */
    private _borderStyle: SLineStyle = SLineStyle.None;
    get borderStyle(): SLineStyle {
        return this._borderStyle;
    }
    set borderStyle(v: SLineStyle) {
        this._borderStyle = v;
        this.update();
    }

    /** 原点位置    */
    private _originPosition: STextOrigin = STextOrigin.LeftTop;
    get originPosition(): STextOrigin {
        return this._originPosition;
    }
    set originPosition(v: STextOrigin) {
        this._originPosition = v;
        this.update();
    }

    /** 文本绘制最大宽 */
    maxWidth: number | undefined = undefined;

    /**
     * 构造函数
     *
     * @param   parent      指向父Item
     * @param   str         文本内容
     */
    constructor(parent: SGraphItem | null, str: string = "") {
        super(parent);
        this.showSelect = false;
        this._text = str;
        this._font = new SFont("sans-serif", 12);
        this.height = 12;
    } // Constructor

    /**
     * 对象边界区域
     *
     * @return  边界区域
     */
    boundingRect(): SRect {
        return new SRect(
            -this.origin.x,
            -this.origin.y,
            this.width,
            this.height
        );
    } // Function boundingRect()

    /**
     * 移动后处理所有坐标,并肩原点置为场景原点
     *
     * @param   x   x坐标
     * @param   y   y坐标
     * */
    moveToOrigin(x: number, y: number): void {
        this.moveTo(this.x + x, this.y + y);
    } // Function moveToOrigin()

    /**
     * 绘制显示状态文本Item
     *
     * @param painter    绘制类
     */
    protected drawShowText(painter: SPainter): void {
        painter.translate(-this.origin.x, -this.origin.y);
        //绘制矩形轮廓
        if (this.selected) {
            painter.shadow.shadowBlur = 10;
            painter.shadow.shadowColor = new SColor(`#00000033`);
            painter.shadow.shadowOffsetX = 5;
            painter.shadow.shadowOffsetY = 5;
        } else {
            painter.shadow.shadowColor = SColor.Transparent;
        }
        painter.brush.color = this.backgroundColor;
        painter.pen.lineWidth = this.lineWidth;
        painter.pen.color = this.strokeColor;
        painter.drawRect(0, 0, this.width, this.height);

        //绘制文本
        painter.brush.color = new SColor(this.color);
        painter.shadow.shadowColor = SColor.Transparent;
        painter.font = this.font;
        this._textArr.forEach((text: string, index: number) => {
            painter.drawText(
                text,
                this.font.size / 4,
                index * (this.font.size * 1.25) + this.font.size / 4,
                this.maxWidth
            );
        });
    } // Function drawShowText()

    /**
     * 根据换行切割文本,绘制多行并计算外轮廓宽高
     *
     */
    protected drawFormatText(): void {
        if (this._painter instanceof SPainter) {
            this._painter.save();
            this._painter.font = this.font;
            let textMaxWidth = 0;
            let fontSize: number = this.font.size;
            this._textArr.forEach((text: string, index: number) => {
                let textWidth: number = this._painter
                    ? this._painter.textWidth(text) + fontSize / 2
                    : fontSize / 2;
                if (textWidth > textMaxWidth) {
                    textMaxWidth = textWidth;
                }
            });
            // 在绘制文本后计算文本的宽高
            this.width = textMaxWidth;
            this.height = fontSize * 1.25 * this._textArr.length + fontSize / 8;
            // 设置原点位置(默认左上角)
            if (this.originPosition == STextOrigin.Centrum) {
                this.origin = new SPoint(this.width / 2, this.height / 2);
            }
            this._painter.restore();
        }
    } // Function drawFormatText()

    /**
     * Item绘制操作
     *
     * @param   painter      绘画类
     */
    onDraw(painter: SPainter): void {
        if (!(this._painter instanceof SPainter)) {
            this._painter = painter;
            this.drawFormatText();
        }
        this.drawShowText(painter);
    } // Function onDraw()
} // Class STextItem