using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisimBox.Scene;
using VisimBox.Renderer;

namespace VisimBuilder.ObjectCreation
{
    public partial class LightCreator : ObjectCreationBase
    {
        public LightCreator()
        {
            InitializeComponent();
            labelObjectName.Text = "Light object";
            textBoxName.Text = "Light";
            lightEdit1.EnablePosAndDir = false;
            lightEdit1.LightChanged += new LightEdit.LightParameterChanged(lightEdit1_LightChanged);
            DisableScale = true;
        }

        void lightEdit1_LightChanged(LightEdit.LightParameter param)
        {
            if (param == LightEdit.LightParameter.Type)
            {
                switch (lightEdit1.SelectedLightType)                    
                {
                    case LightTypes.Ambient:
                    case LightTypes.Position:
                        DisableRotation = true;    
                    break;
                    default:
                        DisableRotation = false;
                        break;
                }
            }
        }

        public override void SetDefault()
        {
            base.SetDefault();
            textBoxName.Text = "Light";
        }

        public override Node ObjectCreation(string name, VisiMath.ITransformation itrans, VisimBox.Scene.Node owner)
        {
            var lightman = VisimBox.VisimEngine.Instance.Renderer.LightMan;
            ILight ilight = null;


            switch (lightEdit1.SelectedLightType)
            {
                case LightTypes.Ambient:
                    ilight = lightman.AddAmbient("lightobject", lightEdit1.AmbientLight);
                    break;
                case LightTypes.Directional:
                    ilight = lightman.AddDirectional("lightobject", new VisiMath.Vector3(), lightEdit1.AmbientLight, lightEdit1.DiffuseLight, lightEdit1.SpecularLight);
                    break;
                case LightTypes.Position:
                    ilight = lightman.AddPositional("lightobject", new VisiMath.Vector3(), lightEdit1.AmbientLight, lightEdit1.DiffuseLight, lightEdit1.SpecularLight);
                    ilight.Attenuation = lightEdit1.Attenuation;
                    break;
                case LightTypes.Spot:
                    ilight = lightman.AddSpot("lightobject", new VisiMath.Vector3(), VisiMath.Vector3.UnitX, lightEdit1.SpotAngle, lightEdit1.AmbientLight, lightEdit1.DiffuseLight, lightEdit1.SpecularLight);
                    ilight.SpotIntensity = lightEdit1.SpotIntensity;
                    ilight.Attenuation = lightEdit1.Attenuation;
                    break;
                default:
                    VisimBox.Console.DebugLine("Unknown type of the light");
                    return null;
            }

            ilight.Radius = lightEdit1.ActiveRadius;
            
            //todo: active area!!!

            LightNode lnode = new LightNode(ilight);
            lnode.Trans = itrans;
            owner.AddNode(lnode, name);
            lnode.UpdateLight(0);

            return lnode;
        }
    }

    public class CreateLightItem : CreateNewItem
    {
        public CreateLightItem(MainBuilderForm boss)
            : base(boss, "Light", "Any of supported light types")
        {
            RefreshBaseCreator();
        }

        protected override void RefreshBaseCreator()
        {
            BaseCreator = new LightCreator();
            BaseCreator.Init(Boss);
        }
    }

}
