import { SObjectItem } from "@persagy-web/graph/";
import { SPainter, SRect, SColor, SFont, SPoint } from "@persagy-web/draw";
import { SGraphItem } from "@persagy-web/graph/";
import { SLineStyle } from "@persagy-web/graph/";
import { STextOrigin } from "@persagy-web/graph/";

/**
 * 文本item
 *
 * @author  韩耀龙(han_yao_long@163.com)
 */
export class EditText extends SObjectItem {
    /** 记录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[] = [];

    /** 文本颜色 */
    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;
        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._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