using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using UnityEngine.Audio;
using UniRx;
using Unity.Linq;
using UnityEngine.SceneManagement;

public class GM : MonoBehaviour
{

    public int points;
    public Text pointsText;
    [SerializeField] private int maxNumOfDigitsInScore;

    public AudioSource musicSrc;
    public IntReactiveProperty HP = new IntReactiveProperty(1);
    public Text HpText;

    public AmmoPool pool;
    public Ammo AmmoPrefab;
    public AmmoPool pool2;
    public Ammo AmmoPrefabEnemy;
    // Start is called before the first frame update
    void Start()
    {
        var player = FindObjectOfType<Player>();
        pool = new AmmoPool(AmmoPrefab);
        Observable.Interval(System.TimeSpan.FromMilliseconds(500)).Subscribe(x =>
        {
            HP.Value = player.DronePoses.Children().Count(y => y.transform.childCount == 0);
        });
        HP.SubscribeToText(HpText);
        HP.Where(x => x == 0).Subscribe(x =>
            {
                //  SceneManager.LoadScene(0);
            });

    }

    // Update is called once per frame
    void Update()
    {
        string scoreString = points.ToString();
        if (scoreString.Length < maxNumOfDigitsInScore)
        {
            string zeros = "";
            int numOfZeros = maxNumOfDigitsInScore - scoreString.Length;
            for (int i = 0; i <= numOfZeros; i++)
            {
                zeros += "0";
            }
            pointsText.text = zeros + scoreString;
        }
        else
        {
            pointsText.text = scoreString;
        }
    }

    public void AddPoints(int pointsToAdd)
    {
        points += pointsToAdd;
    }
}
