import WebSocketServer from './websocketServer.js';
import {BlankROIDrawer, ROIDrawer, StandingDrawer, SpeedLine} from './ROIDrawer.js';

let ROIdrawer = null;
class Player {
    constructor(option) {
        this.ws = null;
        this.options = option;
        this.events = {
            error: ()=>{}
        };
        this.ROI = {
            ROIDrawer,
        }
    }

    init(v) {
        this.options.vue = v;
        //console.log('init');
        ROIdrawer = new ROIDrawer(this.options.drawer);
        this.options.ROI = ROIdrawer;
        this.ws = new WebSocketServer(this.options);
        this.ws.init();
    }

    connect() {
        for(let i in this.events) {
            this.ws.setCallBack(i, this.events[i]);
        }
        this.ws.connect();
    }

    play() {
        //console.log('player')
        // this.close()
        // this.ws = new WebSocketServer(this.options)
        // this.ws.init();
    }

    pause() {
        //console.log('pause')
    }

    close() {
        if(this.ws)
            this.ws.close();
        if(ROIdrawer)
            ROIdrawer.terminate();
        //console.log('close1')
    }

    /**
     * 绘制额外信息
     * @param obj
     */
    updateInfo(obj) {
        this.ws.updateInfo(obj);
    }

    changeROIType(type) {
        ROIdrawer && ROIdrawer.terminate();
        switch (type) {
            case 'Polygon1':
                ROIdrawer = new ROIDrawer(this.options.drawer);
                ROIdrawer.setPolygonNum(1);
                ROIdrawer.cover(this.options.video);
                ROIdrawer.setROIFinishedCallback(this.events['ROIFinished']);
                break;
            case 'Polygon2':
                ROIdrawer = new ROIDrawer(this.options.drawer);
                ROIdrawer.setPolygonNum(2);
                ROIdrawer.cover(this.options.video);
                ROIdrawer.setROIFinishedCallback(this.events['ROIFinished']);
                break;
            case 'Polygon1Line1':
                ROIdrawer = new StandingDrawer(this.options.drawer);
                ROIdrawer.cover(this.options.video);
                ROIdrawer.setROIFinishedCallback(this.events['ROIFinished']);
                break;
            case 'Line2':
                ROIdrawer = new SpeedLine(this.options.drawer);
                ROIdrawer.setLineNum(2);
                ROIdrawer.cover(this.options.video);
                ROIdrawer.setROIFinishedCallback(this.events['ROIFinished']);
                break;
            default:
                ROIdrawer = new BlankROIDrawer(this.options.drawer);
                break;
        }
        this.ws.setROIDrawer(ROIdrawer);
    }


    setROI(data) {
        ROIdrawer && ROIdrawer.setROI(data);
    }

    resetROI() {
        ROIdrawer && ROIdrawer.reset();
    }

    getROIData() {
        if(ROIdrawer) {
            return ROIdrawer.getROIData();
        }
        return null;
    }

    setPolygonNum(num) {
        ROIdrawer && ROIdrawer.setPolygonNum(num);
    }

    /**
     * 自定义事件
     * 目前支持如下事件
     * [error] websocket连接失败
     * [noStream] 收不到码流
     * [canplay] 视频能够播放时触发
     * [initialCompleted] 视频初始化完成, 首次canplay
     * [ROIFinished] 手动绘制ROI完成时触发
     *
     * @param event 事件名
     * @param callback 事件响应函数
     */
    on(event, callback) {
        this.events[event] = callback;
    }
}

export default Player;

