using Assets.Scripts.MapGeneration;
using Assets.Scripts.Utils;
using UnityEngine;
using System;

namespace Assets.Scripts
{
    public class GameController : MonoBehaviour
    {
		public static int PoolSize = 256;
		public static float InitialSpeed = 4f;
		public static float Speed = InitialSpeed;

        public GameObject Wall;
        public GameObject BG;

        private Map _map;

        private Vector3 size;
        private Transform tra;
        private int mult = 1;

        void Start()
        {
            size = BG.GetComponent<SpriteRenderer>().sprite.bounds.size;
            tra = BG.GetComponent<Transform>();
			Screen.autorotateToLandscapeRight = true;
			Screen.autorotateToLandscapeLeft = true;
			Screen.orientation = ScreenOrientation.AutoRotation;
        }

        void Update()
        {
			if (Speed < 16f)
				Speed += 0.0025f;	
            Camera.main.transform.Translate(new Vector3(Speed * Time.deltaTime, 0, 0));
            var rightBorder = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,
                Screen.height, 0)).x;
            var leftBorder = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0)).x;

            if (!(rightBorder > (size.x*mult) - 5)) return;

            BG = (GameObject) Instantiate(BG, new Vector3(size.x + tra.position.x - .5f, tra.position.y, 2), Quaternion.identity);
            tra = BG.GetComponent<Transform>();
            mult++;
        }
    }
}