using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class ThugModel
{
    [SerializeField]
    public string name;

    [SerializeField]
    public Vector3 position;

    public ThugModel(string _name, Vector3 _position)
    {
        name = _name;
        position = _position;
    }
}


public class NPCManager : MonoBehaviour
{
    GameObject NPCCollection;
    public List<ThugModel> thugs;

    public ThugModel bossThug;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void Awake()
    {
        NPCCollection = GameObject.FindGameObjectWithTag("NPCCollection");
        if (NPCCollection == null)
        {
            throw new Exception("Object not found");
        }
        CreateThugs();
        CreateBossThug();
    }

    private void CreateThugs()
    {
        var model = Resources.Load("SampleScene/Enemies/Thug") as GameObject;

        foreach (var thug in thugs)
        {
            var ThugClone = GameObject.Instantiate(model, thug.position, Quaternion.identity, NPCCollection.transform);

            ThugClone.name = thug.name;
            ThugClone.transform.SetParent(NPCCollection.transform);
            ThugClone.GetComponent<FollowingEnemy>().enemyName = thug.name; //delete and set in the controller todo?
            ThugClone.GetComponent<FollowingEnemy>().isKilled = PlayerPrefs.HasKey(thug.name + "-S") ? PlayerPrefs.GetInt(thug.name + "-S") : 0;

            if (PlayerPrefs.HasKey(thug.name + "-S.health"))
            {
                ThugClone.GetComponent<FollowingEnemy>().health = PlayerPrefs.GetFloat(thug.name + "-S.health");
            }

            // Dont set up it manually, let allow logic in Following Enemy scriptmanage avaible itself 
            //ThugClone.SetActive(PlayerPrefs.HasKey(thug.name + "-S") ? !Convert.ToBoolean(PlayerPrefs.GetInt(thug.name + "-S")) : true);
        }
    }

    private void CreateBossThug()
    {
        var modelBossThug = Resources.Load("SampleScene/Enemies/BossThug") as GameObject;

        var BossThugClone = GameObject.Instantiate(modelBossThug, bossThug.position, Quaternion.identity, NPCCollection.transform);
        
        BossThugClone.name = bossThug.name;
        BossThugClone.transform.SetParent(NPCCollection.transform);
        BossThugClone.GetComponent<FollowingEnemy>().enemyName = bossThug.name; //delete and set in the controller todo?
        
        BossThugClone.SetActive(PlayerPrefs.HasKey(bossThug.name + "-S") ? !Convert.ToBoolean(PlayerPrefs.GetInt(bossThug.name + "-S")) : true);

        BossThugClone.GetComponent<FollowingEnemy>().isPanelEnabled = false;
    }

}