using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using System.Text;
using System;

/// <summary>
/// 视频播放同步
/// </summary>
public class FunctionSync_Video : MonoBehaviour
{
    public static Dictionary<string, FunctionSync_Video> dicVideo = new Dictionary<string, FunctionSync_Video>();

    public string id;
    private VideoPlayer videoPlayer;
    private st_Motions st = new st_Motions { m_iOperaType = 10009 };
    /// <summary>
    /// 视频播放器状态
    /// </summary>
    public int State;
    /// <summary>
    /// 当前视频播放时间
    /// </summary>
    private double PlayTime;
    private bool hasInit;

    private int index;
    void Start()
    {
        Init();
    }

    public void Init()
    {
        if (!GameManage.Instance.is单机模式)
        {
            if (!hasInit)
            {
                st.area = LoadManage.Instance.currentRoomArea;
                id = "video_" + gameObject.name;
                videoPlayer = GetComponent<VideoPlayer>();
                byte[] idbyte = Encoding.UTF8.GetBytes(id);
                byte[] tmp = new byte[4 + 4 + 8 + idbyte.Length];
                index = 4 + idbyte.Length;
                //id
                Array.Copy(BitConverter.GetBytes(idbyte.Length), 0, tmp, 0, 4);
                Array.Copy(idbyte, 0, tmp, 4, idbyte.Length);
                //状态
                //时间
                st.m_sOperaData = tmp;
                dicVideo.Add(id, this);
                hasInit = true;
            }
        }
    }
    private void Update()
    {
        //学生回拉,老师不在视频不播放
        if(!GameManage.Instance.is单机模式 )
        {
            if(videoPlayer.isPlaying && videoPlayer.time>PlayTime+1)
            {
                videoPlayer.time = PlayTime+1;
            }
        }
    }

    /// <summary>
    /// 操作同步视频
    /// </summary>
    public void SetAudio(int num)
    {
        State = num;
        if (num == 0)
        {
            //关闭
            videoPlayer.Stop();
            PlayTime = 0;
        }
        else if (num == -1)
        {
            //暂停
            videoPlayer.playbackSpeed=0;
            PlayTime = videoPlayer.time;
        }
        else if (num == -2)
        {
            //继续
            videoPlayer.playbackSpeed = 1;
            PlayTime = videoPlayer.time;
        }
        else if(num==1)
        {
            //播放
            videoPlayer.playbackSpeed = 1;
            videoPlayer.Play();
            PlayTime = videoPlayer.time;
        }

        SendSync();
    }

    /// <summary>
    /// 回调(老师不执行)
    /// </summary>
    /// <param name="state"></param>
    /// <param name="time"></param>
    public void CallBack(int state,double time)
    {
        State = state;
        PlayTime = time;
        if (State == 0)
        {
            //关闭
            videoPlayer.Stop();
        }
        else if (State == -1)
        {
            //暂停
            videoPlayer.playbackSpeed = 0;
        }
        else if(State==1)
        {
            //播放
            videoPlayer.playbackSpeed = 1;
            videoPlayer.Play();
        }
        videoPlayer.time = time;
    }

    public void JoinRoomCallBack(int state, double time)
    {
        State = state;
        PlayTime = time;
        if (State == -1)
        {
            //暂停
            videoPlayer.Play();
            videoPlayer.playbackSpeed = 1;
            videoPlayer.time = time;
            videoPlayer.playbackSpeed = 0;
        }
        else if (State == 1)
        {
            //播放
            videoPlayer.Play();
            videoPlayer.playbackSpeed = 1;
            videoPlayer.time = time;
        }
    }

    

    /// <summary>
    /// 更新时间
    /// </summary>
    /// <param name="time"></param>
    public void SetCurrentTime(double time)
    {
        PlayTime = time;
        SendSync();
    }

    private void SendSync()
    {
        //状态
        Array.Copy(BitConverter.GetBytes(State), 0, st.m_sOperaData, index, 4);
        //时间
        Array.Copy(BitConverter.GetBytes(PlayTime), 0, st.m_sOperaData, index + 4, 8);
        LoadManage.Instance.RSclient.Send(st);
    }
}