using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.InteractionSystem;
using System.Collections.Generic;
using System.Linq;

namespace Project.World
{
    public class Loadable : CoreBehaviour
    {
        public float loadTime = 10f;
        public float unloadTime = 5f;
        public float loadDownDistance = 10f;

        private Vector3 truePosition;
        private new Renderer renderer;
        public bool loaded = false;
        private Vector3 localOffset; 

        public Vector3 GetStartPosition()
        {
            return this.truePosition;
        }

        public void SetLoaded(bool loaded)
        {
            this.loaded = loaded;
        }

        // Use this for initialization
        protected override void InnerAwake()
        {
            
        }

        protected override void InnerStart()
        {
            if (this.transform.parent != null)
            {
                this.localOffset = this.transform.position - this.transform.parent.position;
                this.truePosition = this.transform.parent.position + this.localOffset;
            }
            else
            {
                this.truePosition = this.transform.position;
            }
            Vector3 offsetTruePosition = this.truePosition + Vector3.up * -this.loadDownDistance;
            this.transform.position = offsetTruePosition;

            this.renderer = GetComponent<MeshRenderer>();
            if (this.renderer == null)
            {
                this.renderer = GetComponent<SpriteRenderer>();
            }
            if (this.renderer != null)
            {
                this.renderer.enabled = false;
            }

            Announce(new WorldLoader.AddLoadableAction(this));
        }

        // Update is called once per frame
        protected void LateUpdate()
        {
            if (this.transform.parent != null)
            {
                this.truePosition = this.transform.parent.position + this.localOffset;
            }
            Vector3 offsetTruePosition = this.truePosition + Vector3.up * -this.loadDownDistance;
            if (this.loaded)
            {
                if (Vector3.Distance(this.transform.position, truePosition) > 0.01f)
                {
                    this.transform.position = Vector3.Lerp(this.transform.position, truePosition, Time.deltaTime * this.loadTime);
                }
                else
                {
                    this.transform.position = this.truePosition;
                }
                if (this.renderer != null)
                {
                    this.renderer.enabled = true;
                }
                //this.collider.enabled = true;
            }
            else
            {
                if (Vector3.Distance(offsetTruePosition, this.transform.position) > 1f)
                {
                    this.transform.position = Vector3.Lerp(this.transform.position, offsetTruePosition, Time.deltaTime * this.unloadTime); ;
                }
                else
                {
                    if (this.renderer != null)
                    {
                        this.renderer.enabled = false;
                    }
                }
                //this.collider.enabled = false;
            }
        }
    }
}