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 VisiMath;

namespace VisimBuilder
{
    public partial class LightsBrowser : UserControl
    {
        MainBuilderForm boss;

        LightNode MyNode;

        public LightsBrowser()
        {
            InitializeComponent();
        }

        public void Init(MainBuilderForm myboss)
        {
            boss = myboss;
            boss.SelectedNodeChanged += new MainBuilderForm.NodeChangedCallback(boss_SelectedNodeChanged);
            RefreshList();
            lightEdit1.EnableDirectChange = true;
            lightEdit1.EnablePosAndDir = false;
            lightEdit1.LightChanged += new LightEdit.LightParameterChanged(lightEdit1_LightChanged);
        }

        void lightEdit1_LightChanged(LightEdit.LightParameter param)
        {
            if (MyNode != null)
                MyNode.UpdateLight(0);
        }

        private void Startup(object sender, EventArgs e)
        {
        }


        void boss_SelectedNodeChanged()
        {
            var ln = boss.SelectedNode as LightNode;

            if (ln == null)
                return;
            MyNode = ln;
            UpdateFromNode();
        }

        void RefreshList()
        {
            this.listBox1.Items.Clear();
            RefreshNode(VisimBox.VisimEngine.Instance.MainScene);

        }

        void RefreshNode(Node node)
        {
            var light = node as LightNode;
            if (light != null)
            {
                listBox1.Items.Add(light.FullName);
            }

            foreach (var n in node)
            {
                RefreshNode(n);
            }
        }


        void UpdateFromNode()
        {
            if (MyNode == null)
            {
                lightEdit1.AttachedLight = null;
                return;
            }
            lightEdit1.AttachedLight = MyNode.LightObject;
        }

        private void LightListSelected(object sender, EventArgs e)
        {
            string name = (string)listBox1.SelectedItem;

            var node = VisimBox.VisimEngine.Instance.MainScene.GetChildByFullName(name);
            if (node != null)
            {
                MyNode = node as LightNode;
                UpdateFromNode();
            }
            else
            {
                VisimBox.Console.WriteLine("Error when selecting light?!");
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            LightListSelected(sender, e);
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void buttonnew_Click(object sender, EventArgs e)
        {
            var lm = VisimBox.VisimEngine.Instance.Renderer.LightMan;

            var light = lm.AddDirectional("newlight");

            LightNode newnode = new LightNode(light);
            newnode.Trans = new QTransformation();
            VisimBox.VisimEngine.Instance.MainScene.AddNode(newnode);
            RefreshList();
            MyNode = newnode;
            UpdateFromNode();
        }




    }
}
