/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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 { SGraphEdit, SBaseCirclePolylineEdit } from "@persagy-web/edit";
import { SGraphItem } from "@persagy-web/graph/lib";
import { SMouseEvent } from "@persagy-web/base/lib";
import { Marker } from "@persagy-web/big/lib";
import { SColor,SPath, SFont, SPainter, SPoint, SRect, SSize } from "@persagy-web/draw/lib";

/**
 * 编辑基础管道接头类
 *
 * @author 韩耀龙 <han_yao_long@163.com>
 */
export class SBasePipeUninTool extends SGraphEdit {

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //属性
    // 数据
    data: Marker;
    // 宽高
    width: number = 64;
    height: number = 64;
    // 轮廓点
    outLineTpye: any = {
        'wantou': 2,
        'santong': 3,
        'sitong': 4,
    }

    // 旋转角度
    _angle:number = 0;
    get angle(): number {
        return this._angle;
    }
    set angle(v: number) {
        this._angle = v;
        this.update();
    }

    /** path 对象 */
    path: SPath = new SPath();
    /** 轮廓点 */
    outLineLine: SPoint[] = [];

    /** 填充色 */
    _fillColor: SColor = new SColor('#2196f3');
    get fillColor(): SColor {
        return this._fillColor;
    }
    set fillColor(v: SColor) {
        this._fillColor = v;
        this.update();
    }

    /** 弧度 */
    _radius:number = 0;
    get radius(): number {
        return this._radius;
    }
    set radius(v: number) {
        this._radius = v;
        this.generatePath(this.outLineLine,v)
        this.update();
    }

    /** 线条宽度 */
    _lineWidth: number = 20;
    get lineWidth(): number {
        return this._lineWidth;
    }
    set lineWidth(v: number) {
        this._lineWidth = v;
        this.update();
    }

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      数据
     */
    constructor(parent: SGraphItem | null, data: Marker) {
        super(parent);
        this.data = data;
        this.initData(data);
    } // Constructor

    /**
     * 初始化数据
     *
     * @param data Mark
     */
    initData(data: Marker) {
        this.width = data.style.default.width ? data.style.default.width : 64;
        this.height = data.style.default.height ? data.style.default.height : 64;
        this.moveTo(data.pos.x, data.pos.y);
        // 判断为几通管道
        this.computeUninNumber(this.outLineTpye[data.style.uninToolType]);
        this.zOrder = this.data.style.default.zorder ? this.data.style.default.zorder : this.zOrder;
        // this.fillColor =  this.data.style.default.fillColor ?new SColor(this.data.style.default.fillColor) : this.fillColor;
        this.angle = this.data.style.default.angle ? this.data.style.default.angle :0
        this.radius  = this.data.style.default.radius ? this.data.style.default.radius :  this.radius  ;
        this.lineWidth = this.data.style.default.lineWidth ? this.data.style.default.lineWidth : this.lineWidth ;

        this.update()
    }

    /**
     * 鼠标按下事件
     *
     * @param event   保存事件参数
     * @return 是否处理该事件
     */
    onMouseDown(event: SMouseEvent): boolean {
        super.onMouseDown(event);
        return true;
    }
    /**
     * 实时变化宽高
     *
     * @param oldSize 旧大小
     * @param newSize 新大小
     */
    resize(oldSize: SSize, newSize: SSize) {
        this.width = newSize.width;
        this.height = newSize.height;
        this.computeUninNumber(this.outLineTpye[this.data.style.uninToolType]);
        // this.outLine.pointList = this.outLineLine; //赋值轮廓
        this.update();
    }

    /**
     * Item 对象边界区域
     *
     * @return 对象边界区域
     */
    boundingRect(): SRect {
        return new SRect(0, 0, this.width, this.height);
    }

    /**
     * 计算对应点位
     * @param type 接口数量
     */
    computeUninNumber(type: number) {
        //  如果为二通
        const topPoint = new SPoint(this.width / 2, 0);
        const centerPoint = this.boundingRect().center();
        const rightPoint = new SPoint(this.width, this.height / 2);
        const leftPoint = new SPoint(0, this.height / 2);
        const bottomPoint = new SPoint(this.width / 2, this.height)
        if (2 == type) {
            this.outLineLine = [topPoint, centerPoint, rightPoint];
        } else if (3 == type) {
            this.outLineLine = [topPoint, centerPoint, rightPoint, leftPoint];
        } else if (4 == type) {
            this.outLineLine = [topPoint, centerPoint, rightPoint, leftPoint, centerPoint, bottomPoint];
        }
        this.generatePath(this.outLineLine,this.radius)
    }


    /**
     * 返回对象储存的相关数据
     *
     * @return 相关数据
     */
    toData(): any {

        if (!this.data) return;
        if (this.data.size) {
            this.data.size.width = this.width;
            this.data.size.height = this.height;
        } else {
            this.data.size = {
                width: this.width,
                height: this.height
            }
        }

        this.data.pos.x = this.pos.x;
        this.data.pos.y = this.pos.y;
        this.data.style.default.zorder = this.zOrder;
        this.data.style.default.fillColor = this.fillColor.value;
        this.data.style.default.angle = this.angle;
        this.data.style.default.radius = this.radius;
        this.data.style.default.lineWidth = this.lineWidth;
        return this.data;
    }

    /**
     * 根据点集生成 path 对象
     *
     * @param list  需要生成的点击
     * @param r     拐点处圆角半径
     */
    generatePath(list: SPoint[], r: number = 0): void {
        const len = list.length;
        // 当前这小有效
        if (len) {
            this.path = new SPath();
            this.path.moveTo(list[0].x, list[0].y);
            // 遍历点集列表
            for (let i = 1; i < len - 1; i++) {
                const temp = list[i];
                const next = list[i + 1];
                this.path.arcTo(temp.x, temp.y, next.x, next.y, r);
            }
            const last = list[len - 1];
            this.path.lineTo(last.x, last.y);
        }
    }

    /**
     * Item 绘制操作
     *
     * @param painter   绘画对象
     */
    onDraw(painter: SPainter): void {
        super.onDraw(painter);
        const topPoint = this.outLineLine[0];
        const rightPoint = this.outLineLine[2];
        const leftPoint = this.outLineLine[3];
        const bottomPoint = this.outLineLine[5];
        const pipeWidth = this.lineWidth;
        painter.pen.color = this.fillColor;
        painter.pen.lineWidth = this.lineWidth;
        //设置旋转角度
        if(this.angle != 0){
            painter.rotate(this.angle);
        }


        painter.drawPath(this.path);
        const lineWidth = this.lineWidth / 4;
        painter.pen.lineWidth = lineWidth;

        // 绘制装饰线
        if (3 == this.outLineLine.length) {
            painter.drawLine(topPoint.x - pipeWidth, topPoint.y, topPoint.x + pipeWidth, topPoint.y);
            painter.drawLine(rightPoint.x, rightPoint.y - pipeWidth, rightPoint.x, rightPoint.y + pipeWidth);
        } else if (4 == this.outLineLine.length) {
            painter.drawLine(topPoint.x - pipeWidth, topPoint.y, topPoint.x + pipeWidth, topPoint.y);
            painter.drawLine(rightPoint.x, rightPoint.y - pipeWidth, rightPoint.x, rightPoint.y + pipeWidth);
            painter.drawLine(leftPoint.x, leftPoint.y - pipeWidth, leftPoint.x, leftPoint.y + pipeWidth);
        } else {
            painter.drawLine(topPoint.x - pipeWidth, topPoint.y, topPoint.x + pipeWidth, topPoint.y);
            painter.drawLine(rightPoint.x, rightPoint.y - pipeWidth, rightPoint.x, rightPoint.y + pipeWidth);
            painter.drawLine(leftPoint.x, leftPoint.y - pipeWidth, leftPoint.x, leftPoint.y + pipeWidth);
            painter.drawLine(bottomPoint.x - pipeWidth, bottomPoint.y, bottomPoint.x + pipeWidth, bottomPoint.y);
        }

    }
}