using System;
using CriWare;
using CriWare.CriMana;
using UnityEngine;

namespace Cute;

public class MoviePlayer : MonoBehaviour
{
	[SerializeField]
	private CriManaMovieMaterial _movieController;

	[SerializeField]
	private Camera _camera;

	public event Action CallbackReady;

	public event Action CallbackStart;

	public event Action CallbackEnd;

	private void Update()
	{
		if (_movieController.player == null)
		{
			return;
		}
		switch (_movieController.player.status)
		{
		case Player.Status.Playing:
			if (this.CallbackStart != null)
			{
				this.CallbackStart();
				this.CallbackStart = null;
			}
			break;
		case Player.Status.PlayEnd:
			Stop();
			if (this.CallbackEnd != null)
			{
				this.CallbackEnd();
				this.CallbackEnd = null;
			}
			break;
		}
	}

	public void Load(string filePath)
	{
		filePath = filePath.Replace("file:///", "");
		_movieController.maxFrameDrop = CriManaMovieMaterial.MaxFrameDrop.Ten;
		_movieController.player.Stop();
		_movieController.player.SetFile(null, filePath);
		_movieController.player.Prepare();
		if (this.CallbackReady != null)
		{
			this.CallbackReady();
			this.CallbackReady = null;
		}
	}

	public void Play()
	{
		_camera.enabled = true;
		if (IsPaused())
		{
			_movieController.player.Pause(sw: false);
		}
		else
		{
			_movieController.player.Start();
		}
	}

	public void Stop()
	{
		QualitySettings.vSyncCount = 0;
		_camera.enabled = false;
		_movieController.player.Stop();
	}

	public void Pause()
	{
		_movieController.player.Pause(!_movieController.player.IsPaused());
	}

	public void SetVolume(float volume)
	{
		_movieController.player.SetVolume(volume);
	}

	public int GetSeekPosition()
	{
		return (int)(_movieController.player.GetTime() / 1000);
	}

	public int GetDuration()
	{
		int totalFrames = (int)_movieController.player.movieInfo.totalFrames;
		int num = (int)(_movieController.player.movieInfo.framerateN / 1000);
		if (num == 0)
		{
			return -1;
		}
		return totalFrames / num * 1000;
	}

	public int GetCurrentSeekPercent()
	{
		return GetSeekPosition() / GetDuration() * 100;
	}

	public void SeekTo(int seek)
	{
		int num = (int)(_movieController.player.movieInfo.framerateN / 1000);
		int seekPosition = seek * num / 1000;
		_movieController.player.Stop();
		_movieController.player.SetSeekPosition(seekPosition);
		_movieController.player.Start();
	}

	public bool IsReady()
	{
		return _movieController.player.status == Player.Status.Ready;
	}

	public bool IsPlaying()
	{
		if (_movieController.player.status == Player.Status.Playing)
		{
			return !IsPaused();
		}
		return false;
	}

	public bool IsPaused()
	{
		return _movieController.player.IsPaused();
	}

	public bool IsStopped()
	{
		return _movieController.player.status == Player.Status.Stop;
	}

	public bool IsFinished()
	{
		return _movieController.player.status == Player.Status.PlayEnd;
	}

	public bool IsError()
	{
		return _movieController.player.status == Player.Status.Error;
	}
}
