import { useState, useEffect } from 'react';
import moment from 'moment';
import _ from 'lodash';
function changeUrlArg(url, arg, val){
  var pattern = arg+'=([^&]*)';
  var replaceText = arg+'='+val;
  return url.match(pattern) ? url.replace(eval('/('+ arg+'=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url+'&'+replaceText : url+'?'+replaceText);
}


function getPlayUrl(url, begin,end){ //为没有视频的分段手动添加上播放路径
  let playUrl = _.cloneDeep(url)
  playUrl= changeUrlArg(playUrl,'begin',begin)
  playUrl= changeUrlArg(playUrl,'end',end)
  return playUrl
}
export function formatHistoryList(playlist, startTime, endTime,isFrontEnd) {
  if (!Array.isArray(playlist) || playlist.length === 0) {
    return {
      beginDate: 0,
      duration: 0,
      fragments: [],
    };
  }
  let fragment = {};
  const playListBeginDate = playlist[0]?.beginTime
  const playListEndDate = playlist[playlist.length - 1]?.endTime
  if(playListBeginDate<startTime){
    startTime=playListBeginDate
  }
  if(playListEndDate>endTime){
    endTime=playListEndDate
  }
  fragment.beginDate = startTime * 1000;
  fragment.duration = endTime - startTime;
  fragment.fragments = [];

  let fragments = [];
  // 有历史视频处理断片时间
  if (+playlist[0].beginTime != startTime) {
    let item = {
      begin: 0,
      end: playlist[0].beginTime - startTime,
    }
    if(isFrontEnd&&playlist[0]?.url){
      let url = getPlayUrl( playlist[0].url,startTime,playlist[0].beginTime)
      item.file=url
      item.controlUrl=playlist[0].controlUrl
      item.isPiece = true
    }
    fragments.push(item);
  }
  playlist.forEach((v, k) => {
    //begin----处理有视频部分
    fragments.push({
      begin: v.beginTime - startTime,
      end: v.endTime - startTime,
      file: v.url || v.flv,
      controlUrl: v.controlUrl,
    });
    //end----处理有视频部分
    //begin----处理无视频部分
    const nextData = playlist[k + 1];
    if (nextData) {
      //处理存在的，视频断片数据
      if (nextData.beginTime < v.endTime) {
        console.error('数据有问题，下一个的视频开始时间小于上一个视频的结束时间');
      }
      //下一个的begin大于上一个的end，就是存在断片
      if (nextData.beginTime > v.endTime) {
        let item  = {
          begin: v.endTime - startTime,
          end: nextData.beginTime - startTime,
        }
        if(isFrontEnd&&playlist[0]?.url){
          let url = getPlayUrl( playlist[0].url,v.endTime,nextData.beginTime)
          item.file=url
          item.isPiece = true
        }
        fragments.push(item);
      }
    }
    //end----处理无视频部分
  });

  let lastItem = playlist[playlist.length - 1];
  if (lastItem.endTime < endTime) {
    console.log('要补');
    let item  = {
      begin: lastItem.endTime - startTime,
      end: endTime - startTime,
    }
    if(isFrontEnd&&playlist[0]?.url){
      let url = getPlayUrl( playlist[0].url,lastItem.endTime,endTime)
      console.log(url);
      item.file=url
      item.isPiece = true
    }
    fragments.push(item);
  }
  fragment.fragments = fragments;
  return fragment;
}

export function getLoopBoxSize(num) {
  const libScreen = [
    { value: 1, label: "单屏", icon: "icon-S_View_ArrayNum", size: "100%" },
    { value: 4, label: "4分屏", icon: "icon-S_View_ArrayNum2", size: "50%" },
    { value: 9, label: "9分屏", icon: "icon-S_View_ArrayNum1", size: "33.3333%" },
    { value: 16, label: "16分屏", icon: "icon-S_View_ArrayNum3", size: "25%" }
  ];
  return libScreen.find((v) => v.value === num).size;
}

export function getTimeRang(startTime, endTime) {
  const start = startTime ? moment(startTime * 1000) : moment().subtract(1, 'hour');
  const end = endTime ? moment(endTime * 1000) : moment();
  return [start, end];
}

export function useBarStatus(player) {
  const [barStatus, setBarStatus] = useState('show')
  useEffect(() => {
    function showBar() {
      setBarStatus('show')
    }
    function hideBar() {
      setBarStatus('hide')
    }

    if (!player) {
      return setBarStatus('hide')
    }
    setBarStatus('show')

    player.on('showContraller', showBar)
    player.on('hideContraller', hideBar)
    return () => {
      player.off('showContraller', showBar)
      player.off('hideContraller', hideBar)
    }
  }, [player])
  return barStatus
}
