using DG.Tweening;
using System;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;

namespace FL.Map
{
    public class TestMainMapVM : IMapViewModel
    {
        public MeshRenderer BgMesh { get; private set; }
        public MeshRenderer CloudMesh { get; private set; }
        public Transform MapRoot { get; private set; }
        public Transform PlayerRoot { get; private set; }
        public Transform EnemyRoot { get; private set; }
        public Transform BulletRoot { get; private set; }
        public Transform VfxRoot { get; private set; }
        public Transform UIRoot { get; private set; }
        public ParallelCamera Camera { get; private set; }
        public SpriteRenderer BlackMask { get; private set; }

        private Vector3 _cameraDefault;
        private Vector3 _mapRootDefault;
        public TestMainMapVM(IObjectCollector collector)
        {
            BgMesh = collector.GetComponent<MeshRenderer>("BgMesh");
            CloudMesh = collector.GetComponent<MeshRenderer>("CloudMesh");
            MapRoot = collector.GetGameObject("MapRoot").transform;
            PlayerRoot = collector.GetGameObject("PlayerRoot").transform;
            EnemyRoot = collector.GetGameObject("EnemyRoot").transform;
            BulletRoot = collector.GetGameObject("BulletRoot").transform;
            VfxRoot = collector.GetGameObject("VfxRoot").transform;
            UIRoot = collector.GetGameObject("UIRoot").transform;
            Camera = collector.GetComponent<ParallelCamera>("MainCamera");
            BlackMask = collector.GetComponent<SpriteRenderer>("BlackMask");
            _cameraDefault = Camera.transform.localPosition;
            _mapRootDefault = MapRoot.localPosition;
            _bgMaterial = BgMesh.material;
            _bgSpeed = _bgMaterial.GetFloat("_ScrollY");
            BgMesh.material = _bgMaterial;
            _cloudMat = CloudMesh.material;
            _cloudSpeed = _cloudMat.GetFloat("_ScrollY");
            CloudMesh.material = _cloudMat;
        }
        public void Reset()
        {
            //BlackMask.gameObject.SetActive(false);
            Camera.transform.localPosition = _cameraDefault;
            MapRoot.localPosition = _mapRootDefault;
            _moveTime = 0;
            _targetOffset = Vector3.zero;
            SetCloudSpeed(cloud_default_speed);
        }
        #region 地图滚动
        private const float cloud_default_speed = 0.01f;
        private Material _bgMaterial;
        private Material _cloudMat;
        private float _bgSpeed;
        private float _cloudSpeed = cloud_default_speed;
        private int _moveTime = 0;
        private Vector3 _targetOffset = Vector3.zero;
        public void SetFocusPosition(Vector3 position)
        {
            _targetOffset = MapRoot.position - position;
        }
        public void UpdateMapMoveTime(int millisecond)
        {
            if (Camera.Target?.IsMoving ?? false)
            {
                if (_targetOffset != Vector3.zero)
                {
                    MapRoot.position = Camera.Target.Position + _targetOffset;
                }
                _moveTime += millisecond;
                _bgMaterial.SetFloat("_MoveTime", _moveTime * 0.001f);
                if (_cloudSpeed != _bgSpeed)
                {
                    SetCloudSpeed(_bgSpeed);
                }
            }
            else if (_cloudSpeed == _bgSpeed)
            {
                SetCloudSpeed(cloud_default_speed);
            }
        }
        private void SetCloudSpeed(float speed)
        {
            _cloudSpeed = speed;
            _cloudMat.SetFloat("_ScrollY", speed);
        }
        #endregion
        /// <summary>
        /// 显示黑屏
        /// </summary>
        /// <param name="duration">单位:秒</param>
        /// <param name="callback"></param>
        public void ShowBlackMask(float duration, Action callback)
        {
            BlackMask.gameObject.SetActive(true);
            var color = Color.black;
            color.a = 0;
            //BlackMask.color = color;
            UIRoot.gameObject.SetActive(false);
            var tween = DOTween.ToAlpha(() => color, (a) => { BlackMask.color = a; }, 1, duration);
            tween.onComplete = () =>
            {
                UIRoot.gameObject.SetActive(true);
                callback.SafeInvoke();
                var tween1 = DOTween.ToAlpha(() => color, (a) => { BlackMask.color = a; }, 0, duration);
                tween1.onComplete = () =>
                {
                    BlackMask.gameObject.SetActive(false);
                };
            };
        }
    }
}