// import { MovieClip } from './../../libs/svga/MovieClip';
// import { Parser } from './../../libs/svga/parser';
export default class SVGACtrl {

    static _instance: SVGACtrl;

    public SVGAPool: any[] = [];

    public static get instance() { return SVGACtrl._instance || (SVGACtrl._instance = new SVGACtrl()) }

    public load(name, path) {
        return new Promise((resolve, reject) => {
            let parser = new window['SVGA'].Parser();
            let _path = window['svgaPath'] + path + "";
            parser.load(_path, (videoItem) => {
                var mv = new window['SVGA'].EgretMovieClip(videoItem)

                this.SVGAPool[name] = videoItem;

                console.log(this.SVGAPool)
                let fun
                mv.addEventListener(egret.Event.COMPLETE, fun = function () {
                    // console.log("svga加载完成"
                    // mv.removeEventListener(egret.Event.COMPLETE, fun, this)
                    // mv.stop()
                    // mv.startAniRange(30, 60);

                }, this)
                console.log(`${name}加载完成`)
                resolve();
            }, function (error) {
                reject(error.message)
            })
        })
    }

    getSVGAByName(name) {
        // debugger
        console.log(this.SVGAPool)
        if (this.SVGAPool[name + ""]) {
            return this.SVGAPool[name];
        } else {

            return null;
        }
        // this.SVGAPool.forEach(svga => {

        //     if (svga[name]) {
        //         return svga[name];
        //     }
        // })
    }

    public play() {

    }

    /**
     * 深度拷贝动画帧
     * @param frames 待拷贝的动画帧
     * @param tx x偏移量
     * @param ty y偏移量
     * @param a scaleX偏移量
     * @param d scaleY偏移量
     */
    public static deepCopyFrames(frames, tx, ty, scaleX = 1, scaleY = 1): any[] {
        var cf = [];
        for (var j = 0; j < frames.length; j++) {
            var frame = frames[j];
            const pt = frame.transform;
            var f: { alpha: number, transform: any } = { alpha: 0, transform: null };
            //透明度
            f.alpha = frame.alpha;
            f.transform = {
                a: scaleX * pt.a,
                b: scaleX * pt.b,
                c: scaleY * pt.c,
                d: scaleY * pt.d,
                tx: (tx * pt.a) + (ty * pt.c) + pt.tx,
                ty: (tx * pt.b) + (ty * pt.d) + pt.ty,
            };
            cf.push(f)
        }
        return cf;
    }
}
