/*
 * @Author: 634665781 634665781@qq.com
 * @Date: 2023-04-06 11:18:25
 * @LastEditors: 634665781 634665781@qq.com
 * @LastEditTime: 2023-04-12 14:29:59
 * @FilePath: \ui\src\components\VideoEsy\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import EZUIKit, { log } from 'ezuikit-js';
import React, { useState, useRef, useEffect, useImperativeHandle, forwardRef } from 'react';
import axios from 'axios';
const VideoEsy = (props, ref) => {
  const _video1 = useRef(null);
  //console.log(props,'props');
  const { appKey, appSecret, key } = props.VideoInfo;
  const [jessibuca, setPlayers] = useState(null);

  const getToken = async () => {
    axios
      .post(
        `https://open.ys7.com/api/lapp/token/get?appKey=${appKey}&appSecret=${appSecret}`,
        {},
        {
          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/${key}/1.hd.live`,
        });
        setPlayers(PlayersA);
        useImperativeHandle(props.Appref, () => {
          // changeVal 就是暴露给父组件的方法, newVal是父组件传递的参数
          return jessibuca; // _video&& _video.current&&_video.current.jessibuca
        });
      })
      .catch(function (error) {
        console.log(error);
      });
  };

  useEffect(() => {
    getToken();
  }, []);

  return <div id="video-container"></div>;
};
const $VideoEsy = forwardRef(VideoEsy);
export default $VideoEsy;
