/*
 * Flow Controller
*/
 
import Event from '../events';
import EventHandler from '../event-handler'; 

class FlowController extends EventHandler {

  constructor(wfs) {
    super(wfs,
      Event.MEDIA_ATTACHED,
      Event.BUFFER_CREATED,
      Event.FILE_PARSING_DATA,
      Event.FILE_HEAD_LOADED,
      Event.FILE_DATA_LOADED,
      Event.WEBSOCKET_ATTACHED,
      Event.FRAG_PARSING_DATA,
      Event.FRAG_PARSING_INIT_SEGMENT);
    
    this.fileStart = 0;
    this.fileEnd = 0;
    this.pendingAppending = 0;
    this.mediaType = undefined; 
    channelName:this.channelName;
  }

  destroy() {
     EventHandler.prototype.destroy.call(this);
  }  

  onMediaAttached(data) {      
    if (data.websocketName != undefined){
    	
    	     	var query = window.location.search.substring(1);
							var vars = query.split("&");
							var data = {}
							var url = window.location.href;
			                      url = url.split('/')
										var clientType = vars.find(e => {
							              return e.startsWith('clientType')
							          })
										var cardIp = vars.find(e => {
							              return e.startsWith('cardIp')
							          })
										var port = vars.find(e => {
							              return e.startsWith('port')
							          })
										
										var sn = vars.find(e => {
							              return e.startsWith('sn')
							          })
										var demoTime = vars.find(e => {
							              return e.startsWith('demoTime')
							          })
										 var data = {}         
							            data.clientType=clientType.substring(11, clientType.length)
							            
							            data.cardIp=cardIp.substring(7, cardIp.length)
							            
							            data.port=port.substring(5, port.length)
							            
							            data.sn=sn.substring(3, sn.length)
							            
							            data.demoTime=demoTime.substring(9, demoTime.length)
							    	
							    	console.log(data)
							    	

							//var client = new WebSocket("wss://xcx.androidscloud.com/videoWebSocket?clientType=0&cardIp=30.30.30.24&port=9100&sn=RK3930C2301900042");
                     var urlss = url[2]
      	
        //var client = new WebSocket('ws://192.168.128.254:8022/videoWebSocket?'+"clientType="+data.clientType+"&cardIp="+data.cardIp+"&port="+data.port+"&sn="+data.sn);
        
      var client = new WebSocket("ws://" + urlss + '/videoWebSocket?' + "clientType=" + data.clientType +"&cardIp=" + data.cardIp + "&port=" + data.port + "&sn=" + data.sn);
     
//    var protocol = 'binary';
//    var client = new WebSocket(uri,protocol);
      this.wfs.attachWebsocket(client,data.channelName);
    }else{
       console.log('websocketName ERROE!!!');
    }

  }
  
  onBufferCreated(data) {
    this.mediaType = data.mediaType; 
  }

  onFileHeadLoaded(data) { 
  }

  onFileDataLoaded(data) { 
  }

  onFileParsingData(data) {
  }
 
  onWebsocketAttached(data) {
    this.wfs.trigger(Event.BUFFER_APPENDING, {type: 'video', data: data.payload, parent : 'main'}); 
  }
  
  onFragParsingInitSegment(data) {
  	 var tracks = data.tracks, trackName, track;
 
      track = tracks.video;
      if(track) { 
        track.id = data.id;
      }
 
      for (trackName in tracks) {
        track = tracks[trackName];
        var initSegment = track.initSegment;
        if (initSegment) {
          this.pendingAppending++;
          this.wfs.trigger(Event.BUFFER_APPENDING, {type: trackName, data: initSegment, parent : 'main'});
        }
      }
 
  }

  onFragParsingData(data) {
 
      if(data.type === 'video') {
     
      }
       
      [data.data1, data.data2].forEach(buffer => {
        if (buffer) {
          this.pendingAppending++;
          this.wfs.trigger(Event.BUFFER_APPENDING, {type: data.type, data: buffer, parent : 'main'}); 
        }
      });
 
  }

}
export default FlowController;  
