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

[Serializable]
public class SceneChestBuilder : SceneObjectBuilder<ChestBuildModel>
{
    [SerializeField] int trest = 0;
    protected override string MODEL_SOURCE_PATH { get { return "Chests/"; } }

    public override void Build(ChestBuildModel buildModel)
    {
        GameObject globalGUI = GameObject.FindGameObjectWithTag("ChestCollection");

        if (!globalGUI)
            Debug.LogError("GUI frame not found!!");

        GameObject newObject = Instantiate(buildModel.ChestPrefab, buildModel.Position, Quaternion.identity, globalGUI.transform);

        newObject.name = buildModel.ChestName;
        newObject.transform.SetParent(globalGUI.transform);

    }

/*    public override void Build(ChestPrefabAsset objectToBuild)
    {
        GameObject globalGUI = GameObject.FindGameObjectWithTag("ChestCollection");

        if (!globalGUI)
            Debug.LogError("GUI frame not found!!");


        GameObject newObject = FindModel(objectToBuild.Chest.chestModel.name);

        Debug.Log($"Chest {newObject} - name: {objectToBuild.Chest.chestModel.name}");    
        
        if (!newObject)
            Debug.LogError("Can't find prefarb by name " + objectToBuild.Chest.chestModel.name);

        // Build chest on map
        GameObject chest = Instantiate(newObject, objectToBuild.Position, Quaternion.identity, globalGUI.transform);

        chest.name = objectToBuild.Chest.Name;
        chest.transform.SetParent(globalGUI.transform);

        //Debug.Log("Chest Name: " + chest.name + " Positions: " + chest.transform.position.x + " + " + chest.transform.position.y + " + " + chest.transform.position.z);
        //Debug.Log("Chest Name: " + chest.name + " Local Positions: " + chest.transform.localPosition.x + " + " + chest.transform.localPosition.y + " + " + chest.transform.localPosition.z);
        
        //chest.transform.localScale = new Vector3(1f, 1f, 1f);
        //chest.transform.localPosition = objectToBuild.Position;


        // data about chest cpontent should be handled in SceneChestManager class and pass to ChestUIPanel only after opening Panel by Player
        // in other words objects dont have full info about its data xd
        // confuse and irrational but this way it will be easiet to maintain Data Consistency
    }
*/
    public override GameObject FindModel(string modelName)
    {
        var resource = Resources.Load<GameObject>(MODEL_SOURCE_PATH + modelName);

        if (!resource)
            throw new System.Exception($"Resource {MODEL_SOURCE_PATH + modelName} not found!!");

        return resource;
    }
}