/**
 * 视频相关接口、类
 */
import {callNative, callNativeSync} from './util';
import {isInternalSmallApp} from '../nativeApi/util';

/**
 * Video类
 * 唯一标识 __videoId 标记初始化的video唯一标识
 *
 * @class Video
 */
class Video {
    constructor(id) {
        // 缓存domId
        this._videoId = id;
        const biliComponent = global.__biliComponentData;
        const pageId = global.__biliCacheData.getCurrentPageId();
        if (biliComponent && biliComponent.video && biliComponent.video[pageId] && biliComponent.video[pageId].mapping && biliComponent.video[pageId].mapping[id]) {
            this._uid = biliComponent.video[pageId].mapping[id]
            console.log('this._uid', this._uid);
        }
    }

    // 播放视频
    play() {
        // callNativeSync('appVideo.play', { id: this._videoId });
        Service.postMessage(JSON.stringify({
            type: 'video',
            event: 'play',
            data: {
                videoId: this._videoId
            }
        }), global.__biliCacheData.getCurrentPageId());
    }

    // 暂停视频
    pause() {
        // callNativeSync('appVideo.pause', { id: this._videoId });
        Service.postMessage(JSON.stringify({
            type: 'video',
            event: 'pause',
            data: {
                videoId: this._videoId
            }
        }), global.__biliCacheData.getCurrentPageId());
    }

    // 停止视频
    stop() {
        // callNativeSync('appVideo.stop', { id: this._videoId });
        Service.postMessage(JSON.stringify({
            type: 'video',
            event: 'stop',
            data: {
                videoId: this._videoId
            }
        }), global.__biliCacheData.getCurrentPageId());
    }

    // 跳转到指定位置
    seek(position) {
        // if (!isNaN(position)) {
        //     callNativeSync('appVideo.seek', { id: this._videoId, value: Number(position) });
        // }
        position = Number(position);
        if (Number.isNaN(position)) {
            return;
        }

        Service.postMessage(JSON.stringify({
            type: 'video',
            event: 'seek',
            data: {
                videoId: this._videoId,
                position
            }
        }), global.__biliCacheData.getCurrentPageId());
    }

    // 打开全屏
    requestFullScreen(object) {
        if (object !== undefined && object.direction !== undefined && !Number.isNaN(object.direction)) {
            if (this._uid) {
                callNative('appVideo.requestFullScreen', {
                    id: this._uid,
                    value: object.direction
                });
            }
        }
    }

    // 退出全屏
    exitFullScreen() {
        if (this._uid) {
            callNative('appVideo.exitFullScreen', {
                id: this._uid
            });
        }
    }

    // 发送弹幕
    sendDanmu(data) {
        if (Object.prototype.toString.call(data) === '[object Object]') {
            callNative('appVideo.sendDanmu', {
                id: this._uid,
                value: data,
                success: function () {
                    console.log(true);
                },
                fail: function () {
                    console.log(false);
                }
            });
        }
    }

    // 设置倍速播放
    /*
    * B站：0.5/0.75/1/1.25/1.5/2.0
    * wx：0.5/0.8/1.0/1.25/1.5/2.0
    * **/
    playbackRate(rate) {
        // if (!isNaN(rate)) {
        //     callNativeSync('appVideo.setPlaybackRate', {id: this._videoId,value: Number(rate)});
        // }
        rate = Number(rate);
        if (Number.isNaN(rate)) {
            return;
        }

        Service.postMessage(JSON.stringify({
            type: 'video',
            event: 'playbackRate',
            data: {
                videoId: this._videoId,
                rate
            }
        }), global.__biliCacheData.getCurrentPageId());
    }
    graftPlayer() {
        if(!isInternalSmallApp) {
            throw new Error('api not support')
        }
        const {data} = callNativeSync('appVideo.graftPlayer', {id: this._uid});
        return data
    }

    gotoFreeFlow(obj = {
        url: ''
    }) {
        if(!isInternalSmallApp) {
            throw new Error('api not support')
        }
        callNativeSync('appVideo.gotoFreeFlow', {
            id: this._uid,
            value: obj
        });
    }

    allowWWANPlay() {
        if(!isInternalSmallApp) {
            throw new Error('api not support')
        }
        callNativeSync('appVideo.allowWWANPlay', {id: this._uid});
    }
}

export default {
    /**
     * 创建内部 Video类 上下文 createVideoContext 对象。
     *
     * @returns {Video}
     */
    createVideoContext(id) {
        // const result = callNativeSync('createVideoContext', { id });
        return new Video(id);
    },
    // 小程序新增支持保存视频到本地相册能力
    saveVideoToPhotosAlbum(options = {}){
        callNative('saveVideoToPhotosAlbum', options, { filePath: 'String' });
    }
};
