/*
 * @Author: 634665781 634665781@qq.com
 * @Date: 2023-04-11 13:40:18
 * @LastEditors: 634665781 634665781@qq.com
 * @LastEditTime: 2023-04-26 16:32:17
 * @FilePath: \wisdom-components\packages\base-components\VmsVideo\src\index.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 634665781 634665781@qq.com
 * @Date: 2023-04-11 13:40:18
 * @LastEditors: 634665781 634665781@qq.com
 * @LastEditTime: 2023-04-26 09:42:48
 * @FilePath: \wisdom-components\packages\base-components\TestVideo\src\index.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// eslint-disable-next-line no-unused-vars
import EZUIKit, { log } from 'ezuikit-js';

import axios from 'axios';
import React, { useState, useRef, useEffect, useImperativeHandle, forwardRef } from 'react';
import App1 from './App';
import style from './index.less';
// import * as Jessibuca from './JessibucaVideo/jessibuca';

//  proxy.js配置
// '/jessica/': {
//   target: wsServerPath,
//   changeOrigin: true,
//   ws: true,
//   pathRewrite: {
//     '/jessica': '/jessica',
//   },
// },

const Video = (props, ref) => {
  const _video = useRef(null);
  const Players = useRef(null);
  const { VideoInfo, JessibucaObj } = props;
  let baseUrl = `ws://${window.location.host}/jessica`;

  let _VideoInfo = {
    title: '摄像头',
    // dataRate: 1,
    url: `${baseUrl}/live/s8`,
    fullUrl: '',
    key: `摄像头`,
  };

  const getToken = async () => {
    axios
      .post(
        `https://open.ys7.com/api/lapp/token/get?appKey=${VideoInfo.username}&appSecret=${VideoInfo.password}`,
        {},
        {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
        },
      )
      .then(function (response) {
        let accessToken = response.data.data.accessToken;

        let PlayersA = new EZUIKit.EZUIKitPlayer({
          id: 'video-container', // 视频容器ID
          accessToken: accessToken,
          url: `ezopen://open.ys7.com/${VideoInfo.address}/1.hd.live`,
          templete: 'simple',
          footer: ['talk', 'broadcast', 'hd', 'fullScreen'],
        });

        PlayersA.pause = PlayersA.stop;
        ref.current = PlayersA;
      })

      .catch(function (error) {
        console.log(error);
      });
  };

  if (VideoInfo.protocol != '萤石EZOPEN') {
    useImperativeHandle(ref, () => {
      // changeVal 就是暴露给父组件的方法, newVal是父组件传递的参数
      return _video.current.jessibuca; // _video&& _video.current&&_video.current.jessibuca
    });
  } else {
    getToken();
  }
  const CommonPath = window?.globalConfig?.hasGateWay ? 'PandaCore/GateWay/Video/' : '';
  VideoInfo.url = VideoInfo.useFullUrl
    ? VideoInfo.fullUrl
    : (VideoInfo.pandavmsHost||'ws://'+location.host+'/') + (VideoInfo.gateway
    ? CommonPath
    : '') + 'jessica/' + VideoInfo.id;
  let operateBtns = {
    fullscreen: true,
    screenshot: true,
    play: true,
    record: true,
    audio: true,
  };
  _VideoInfo = { ..._VideoInfo, ...VideoInfo };
  if (JessibucaObj && JessibucaObj.operateBtns) {
    JessibucaObj.operateBtns = {
      ...operateBtns,
      ...JessibucaObj.operateBtns,
    };
  }
  // console.log(_video)
  return (
    <div className={style.app}>
      {VideoInfo.protocol == '萤石EZOPEN' ? (
        <div id="video-container"></div>
      ) : (
        <App1 {...{ VideoInfo: _VideoInfo, JessibucaObj: JessibucaObj }} ref={_video} />
      )}
    </div>
  );
};

const $Video = forwardRef(Video);

export default $Video;