/**
 * cover-svga相关接口、类
 */
import { callNativeSync, callNative, emptyFn } from './util';
import {isInternalSmallApp} from '../nativeApi/util'
import {checkParam} from "./util/commonFn";

/**
 * Video类
 * 唯一标识 __videoId 标记初始化的video唯一标识
 *
 * @class Video
 */
class SVGA {
    constructor(id) {
        // 缓存domId
        this._svgaId = id;
        this._pageId = global.__biliCacheData.getCurrentPageId()
    }

    play(range = [], reverse = false) {
        // api 参数类型校验
        const isOk = checkParam([range, reverse], [
            {
                key: 'range',
                type: 'Array'
            },
            {
                key: 'reverse',
                type: 'Boolean'
            }
        ], {name: 'play'});

        if (isOk) {
            callNativeSync('coversvga.play', {
                id: this._svgaId,
                pageId: this._pageId,
                range: range,
                reverse: reverse
            })
        }
    }

    pause() {
        callNativeSync('coversvga.pause', {
            id: this._svgaId,
            pageId: this._pageId
        })
    }

    stop() {
        callNativeSync('coversvga.stop', {
            id: this._svgaId,
            pageId: this._pageId
        })
    }

    seekToFrame(frame = 0,play = false) {
        // api 参数类型校验
        const isOk = checkParam([frame, play], [
            {
                key: 'frame',
                type: 'Number'
            },
            {
                key: 'play',
                type: 'Boolean'
            }
        ], {name: 'seekToFrame'});

        if (isOk) {
            callNativeSync('coversvga.seekToFrame', {
                id: this._svgaId,
                pageId: this._pageId,
                frame: frame,
                play: play
            })
        }
    }

    seekToPercentage(percentage = 0, play = false) {
        // api 参数类型校验
        const isOk = checkParam([percentage, play], [
            {
                key: 'percentage',
                type: 'Number'
            },
            {
                key: 'play',
                type: 'Boolean'
            }
        ], {name: 'seekToPercentage'});

        if (isOk) {
            callNativeSync('coversvga.seekToPercentage', {
                id: this._svgaId,
                pageId: this._pageId,
                percentage: percentage,
                play: play
            })
        }
    }

    setVisibility(key = '',hidden = false) {
        // api 参数类型校验
        const isOk = checkParam([key, hidden], [
            {
                key: 'key',
                type: 'String'
            },
            {
                key: 'hidden',
                type: 'Boolean'
            }
        ], {name: 'setVisibility'});

        if (isOk) {
            callNativeSync('coversvga.setVisibility', {
                id: this._svgaId,
                pageId: this._pageId,
                key: key,
                hidden: hidden
            })
        }
    }

    setImage(key = '', src = '') {
        // api 参数类型校验
        const isOk = checkParam([key, src], [
            {
                key: 'key',
                type: 'String'
            },
            {
                key: 'src',
                type: 'String'
            }
        ], {name: 'setImage'});

        if (isOk) {
            callNativeSync('coversvga.setImage', {
                id: this._svgaId,
                pageId: this._pageId,
                key: key,
                src: src
            })
        }
    }

    setText(key = '', text = '', size = 16, color = '#000') {
        // api 参数类型校验
        const isOk = checkParam([key, text, size, color], [
            {
                key: 'key',
                type: 'String'
            },
            {
                key: 'text',
                type: 'String'
            },
            {
                key: 'size',
                type: 'Number'
            },
            {
                key: 'color',
                type: 'String'
            }
        ], {name: 'setText'});

        if (isOk) {
            callNativeSync('coversvga.setText', {
                id: this._svgaId,
                pageId: this._pageId,
                key: key,
                text: text,
                font: '',
                size: size,
                color: color
            })
        }
    }

    clearDynamicObjects() {
        callNativeSync('coversvga.clearDynamicObjects', {
            id: this._svgaId,
            pageId: this._pageId,
        })
    }
}

export default {
    /**
     * 创建内部 svga 上下文 createSVGAContext 对象。
     *
     * @returns {Video}
     */
    createSVGAContext(id) {
        return new SVGA(id);
    },
};
