import * as PIXI from 'pixi.js'
import { getCoverRect, getValue } from '../../utils'

export function border(co: any) {
    co.on('update', ({ instance, style }) => {
        const nstyle = getValue(instance, 'layout.newStyle')
        if (!nstyle || nstyle.borderWidth || nstyle.borderRadius) {
            const { borderWidth = 0, borderColor = 0x000000, borderRadius = 0 } = style
            const coverRect = getCoverRect(instance)
            if (coverRect && ['container', 'sprite', 'animatedsprite', 'text'].includes(instance.type)) {
                const { realScale, transform } = instance.layout
                const { width } = instance.getGlobalBounds()

                const _borderWidth = transform.length(width, borderWidth) / realScale.x
                const _borderRadius = transform.length(width, borderRadius)
                const { x: coverX, y: coverY, width: coverWidth, height: coverHeight } = coverRect
                // ---------------------------------------------------------
                // Deal with border
                if (instance._border && instance._border.name === 'border') {
                    instance._border.destroy()
                    instance._border = null
                }
                if (_borderWidth > 0) {
                    // 绘制 border
                    const border = new PIXI.Graphics()
                    const gw = coverWidth - _borderWidth
                    const gh = coverHeight - _borderWidth
                    const gx = - coverWidth / 2
                    const gy = - coverHeight / 2
                    
                    border.lineStyle(_borderWidth, borderColor, 1)
                    if (_borderRadius >= width / 2) {
                        border.drawCircle(0, 0, gw / 2)
                    } else if (_borderRadius > 0) {
                        border.drawRoundedRect(gx, gy, gw, gh, _borderRadius)
                    } else {
                        border.drawRect(gx, gy, gw, gh)
                    }
                    border.name = 'border'

                    instance.addChildAt(border, (instance._background ? 1 : 0))
                    instance._border = border
                }
                // ----------------------------------------------------------
                // Deal with borderRadius
                if (instance.mask && instance.mask.name === 'border-mask') {
                    instance.mask.destroy()
                    instance.mask = null
                }
                if (_borderRadius && _borderRadius > 0) {
                    // 设置 mask
                    const mask = new PIXI.Graphics()
                    mask.beginFill(0xFFFFFF)
                    if (_borderRadius >= width / 2) {
                        mask.drawCircle(0, 0, coverWidth / 2)
                    } else {
                        mask.drawRoundedRect(coverX, coverY, coverWidth, coverHeight, _borderRadius + _borderWidth / 2)
                    }
                    mask.endFill()
                    mask.name = 'border-mask'
                    instance.addChildAt(mask, 0)
                    instance.mask = mask
                }
            }   
        }
    })
}
