using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;

namespace Nan.Scripts
{
    public class ScoreManager : Singleton<ScoreManager>
    {
        private float _score;

        private readonly List<int> _ranking = new List<int>();

        public int CurrentScore { get { return (int)_score; } }
        public int CurrentLayer { get; set; }

        public ScoreManager()
        {
            _ranking.AddRange(Enumerable.Range(0, 10).Select(_ => 0));
        }

        public void AddScore(int score, Vector2 pos)
        {
            var s = score * Mathf.Pow(1.5f, CurrentLayer);
            _score += s;
            ScoreRenderer.Create(pos, (int)s);
        }

        public IEnumerable<int> Ranking { get { return _ranking; } }


        public void Reset()
        {
            var flag = false;
            foreach (var s in _ranking)
            {
                if (_score > s)
                {
                    flag = true;
                }
            }
            if (flag || _ranking.Count < 10)
            {
                _ranking.Add(CurrentScore);
                Debug.Log(CurrentScore);
            }

            _ranking.Sort();
            if (_ranking.Count > 10)
            {
                _ranking.Remove(_ranking.Min());
            }


            _score = 0;
            CurrentLayer = 1;
        }
    }
}