using System;
using System.Collections;
using UnityEngine;
using Wizard;

namespace Cute;

public class MovieManager : MonoBehaviour, IManager
{
	private string fileRootPath;

	private string streamingAssetfileRootPath;

	private MoviePlayer _player;

	private float _volume = 1f;

	private MovieSubtitles _movieSubtitles;

	private IEnumerator Start()
	{
		Toolbox.MovieManager = this;
		while (!CustomPreference.isPreferenceComplete)
		{
			yield return null;
		}
		fileRootPath = "file:///" + Application.persistentDataPath + "/";
		streamingAssetfileRootPath = "file:///" + Application.streamingAssetsPath + "/";
	}

	private void CreateMoviePlayer()
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("Prefab/Movie/MoviePlayer")) as GameObject;
		gameObject.transform.parent = base.transform;
		_player = gameObject.GetComponent<MoviePlayer>();
		_player.SetVolume(_volume);
	}

	public void Load(string filename)
	{
		if (!_player)
		{
			CreateMoviePlayer();
		}
		_player.Load(fileRootPath + filename);
	}

	public void Unload()
	{
		if ((bool)_player)
		{
			UnityEngine.Object.Destroy(_player.gameObject);
		}
	}

	public void Play()
	{
		if (IsReady() || IsPaused() || IsStopped())
		{
			_player.Play();
		}
	}

	public void PlayWithSubtitles(string subtitlesCSV)
	{
		GameObject prefab = (GameObject)Resources.Load("UI/MovieSubtitles");
		_movieSubtitles = NGUITools.AddChild(UIManager.GetInstance().UIManagerRoot.gameObject, prefab).GetComponent<MovieSubtitles>();
		SetCallbackStart(delegate
		{
			_movieSubtitles.PlaySubtitles(subtitlesCSV);
		});
		Play();
	}

	public void Stop()
	{
		if (_movieSubtitles != null)
		{
			_movieSubtitles.Finish();
			_movieSubtitles = null;
		}
		if (IsPlaying() || IsPaused())
		{
			_player.Stop();
		}
	}

	public void Pause()
	{
		if (IsPlaying())
		{
			_player.Pause();
		}
	}

	public void SetVolume(float volume)
	{
		_volume = volume;
		if ((bool)_player)
		{
			_player.SetVolume(volume);
		}
	}

	public int GetSeekPosition()
	{
		if (!_player)
		{
			return 0;
		}
		return _player.GetSeekPosition();
	}

	public int GetDuration()
	{
		if (!_player)
		{
			return 0;
		}
		return _player.GetDuration();
	}

	public int GetCurrentSeekPercent()
	{
		if (!_player)
		{
			return 0;
		}
		return _player.GetCurrentSeekPercent();
	}

	public void SeekTo(int seek)
	{
		if ((bool)_player)
		{
			_player.SeekTo(seek);
		}
	}

	public bool IsReady()
	{
		if (!_player)
		{
			return false;
		}
		return _player.IsReady();
	}

	public bool IsPlaying()
	{
		if (!_player)
		{
			return false;
		}
		return _player.IsPlaying();
	}

	public bool IsPaused()
	{
		if (!_player)
		{
			return false;
		}
		return _player.IsPaused();
	}

	public bool IsStopped()
	{
		if (!_player)
		{
			return false;
		}
		return _player.IsStopped();
	}

	public bool IsFinished()
	{
		if (!_player)
		{
			return false;
		}
		return _player.IsFinished();
	}

	public bool IsError()
	{
		if (!_player)
		{
			return true;
		}
		return _player.IsError();
	}

	public void SetCallbackReady(Action callback)
	{
		if ((bool)_player)
		{
			_player.CallbackReady += callback;
		}
	}

	public void SetCallbackStart(Action callback)
	{
		if ((bool)_player)
		{
			_player.CallbackStart += callback;
		}
	}

	public void SetCallbackEnd(Action callback)
	{
		if ((bool)_player)
		{
			_player.CallbackEnd += callback;
		}
	}
}
