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

namespace Assets.Scripts.Network
{
    public class WorldObject
    {
        public enum WorldObjectType
        {
            wotStatic,
            wotPlayer,
            wotHouse,
            wotCrystal,
            wotLightHouse
        };

        public Vector2 Position;
        public float Orientation;//in degrees
        public float Scale = 1;
        public int PrefabID;
        public int ID;
        public GameObject AssignedGameObject;
        public bool Moved = true;
        public WorldObjectType Type = WorldObjectType.wotStatic;

        public void Instantiate( Manager man )
        {
            if( !man.Prefabs.ContainsKey(PrefabID) )
            {
                Debug.Log("could not instantiate " + PrefabID);
            }
            else
            {
                CreateLocal(man.Prefabs[PrefabID]);
            }
        }

        public void DeInstantiate()
        {
            if( AssignedGameObject != null )
            {
                AssignedGameObject.SetActive(false);
                GameObject.Destroy(AssignedGameObject);
                AssignedGameObject = null;
            }
        }

        public void CreateLocal( GameObject go )
	    {
            go = GameObject.Instantiate( go, new Vector3( Position.x, 0, Position.y), Quaternion.identity);
            if( Scale != 1 )
                go.transform.localScale = go.transform.localScale * Scale;
            go.AddComponent<WorldLink>().wo = this;
		    AssignedGameObject = go;

            if (this is Player) (this as Player).AssignedGameObject.GetComponentInChildren<AnimateEmission>().baseColor = (this as Player).PlayerColor;
        }

        public virtual void UpdateAssignedObject(float FrameTime)
        {
            if ( AssignedGameObject )
            {
                Moved = false;
                AssignedGameObject.transform.SetPositionAndRotation(new Vector3(Position.x, AssignedGameObject.transform.position.y, Position.y), Quaternion.Euler(0, Orientation, 0));
            }
        }
    }
}
