using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using GameCreator.Runtime.Behavior;
using GameCreator.Runtime.Characters;

public class CanBecomeSpider : MonoBehaviour {

    //[SerializeField] private Processor _processor;
    //public Processor processor {
    //    get {
    //        if(_processor == null) {
    //            _processor = GetComponent<Processor>();
    //        }
    //        return _processor;
    //    }
    //}

    //[SerializeField] private Character _character;
    //public Character character {
    //    get {
    //        if(_character == null) {
    //            _character = GetComponent<Character>();
    //        }
    //        return _character;
    //    }
    //}

    private MeshFilter _meshFilter;
    public MeshFilter meshFilter {
        get {
            if(_meshFilter == null) {
                _meshFilter = GetComponent<MeshFilter>();
            }
            return _meshFilter;
        }
    }

    [Button]
    public void BecomeSpider()
    {
        GameObject spiderLegsPrefab = Resources.Load("SpiderEnemyLegs") as GameObject;
        Vector3 meshCenter = transform.TransformPoint(meshFilter.sharedMesh.bounds.center);        
        GameObject spiderLegs = Instantiate(spiderLegsPrefab, meshCenter, transform.rotation);
        spiderLegs.transform.SetParent(transform.parent);
        spiderLegs.gameObject.name += " " + gameObject.name;
        //spiderLegs.transform.localPosition = Vector3.zero;

        // Add Character to parent
        //var character = this.gameObject.AddComponent<Character>();
        //MyExtension.GetCopyOf(character, spiderLegs.GetComponent<Character>());
        //Destroy(spiderLegs.GetComponent<Character>());

        // Add Character to parent
        //var processor = this.gameObject.AddComponent<Processor>();
        //MyExtension.GetCopyOf(processor, spiderLegs.GetComponent<Processor>());
        //Destroy(spiderLegs.GetComponent<Processor>());


        //processor.Restart();
        //character.enabled = true;

        //gameObject.AddComponent<SetTarget>();

        SpiderEnemy spiderEnemy = spiderLegs.GetComponent<SpiderEnemy>();
        spiderEnemy.ParentSpider(transform);

        transform.SetParent(spiderEnemy.transform);

        //character.enabled = true;
        //processor.enabled = true;

    }
}
