﻿using System;
using System.Collections;
using System.Collections.Generic;
using Bolt;
using Ludiq;
using UnityEngine;

namespace BansheeGz.BGDatabase
{
    /// <summary>
    /// return meta name, index and id of provided entity
    /// </summary>
    [UnitCategory("BansheeGz")]
    public class BGDeconstructEntity  : Unit
    {
        [DoNotSerialize] public ValueInput entity;
        [DoNotSerialize] public ValueOutput metaName;
        [DoNotSerialize] public ValueOutput index;
        [DoNotSerialize] public ValueOutput id;
        [DoNotSerialize] public ValueOutput name;
        
        protected override void Definition()
        {
            entity = ValueInput<BGEntity>("entity");
            metaName = ValueOutput("metaName", GetMeta);
            index = ValueOutput("index", GetIndex);
            id = ValueOutput("id", GetId);
            name = ValueOutput("name", GetName);
        }

        private string GetMeta(Flow flow)
        {
            return GetEntity(flow).MetaName;
        }
        private int GetIndex(Flow flow)
        {
            return GetEntity(flow).Index;
        }
        private string GetId(Flow flow)
        {
            return GetEntity(flow).Id.ToString();
        }
        private string GetName(Flow flow)
        {
            return GetEntity(flow).Name;
        }

        private BGEntity GetEntity(Flow flow)
        {
            var value = flow.GetValue<BGEntity>(entity);
            if (value == null) throw new Exception("Can not get an entity- value is not set!");
            return value;
        }
    }
}