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 VisimBuilder.ObjectCreation;

namespace VisimBuilder
{
    public partial class CreateObjectBrowser : UserControl
    {
        protected MainBuilderForm MainForm;
        CreateNewItem GeoObjects, SpecialObjects, MeshManObjects, LightObjects, CameraObjects, HelperObjects, ImportedItems;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TreeView ObjectTree
        {
            get
            {
                return treeObjects;
            }
        }

        public CreateObjectBrowser()
        {
            InitializeComponent();
        }

        public void Init(MainBuilderForm boss)
        {
            MainForm = boss;
            GeoObjects = new CreateNewItem(MainForm, "Geometry objects", "Basic geometry objects");
            treeObjects.Nodes.Add(GeoObjects);

            GeoObjects.Nodes.Add(new CreateQuadItem(MainForm));
            GeoObjects.Nodes.Add(new CreateBoxItem(MainForm));
            GeoObjects.Nodes.Add(new CreatePyramidItem(MainForm));
            GeoObjects.Nodes.Add(new CreateSphereItem(MainForm));
            GeoObjects.Nodes.Add(new CreateRingItem(MainForm));

            SpecialObjects = new CreateNewItem(MainForm, "Special rendering objects", "Special rendering objects like skyboxes");
            SpecialObjects.Nodes.Add(new CreateSkyboxItem(MainForm));
            //todo Billboard
            treeObjects.Nodes.Add(SpecialObjects);

            MeshManObjects = new CreateNewItem(MainForm, "Mesh manager objects", "Geometry objects loaded in mesh manager");
            //todo Meshmanager browser
            treeObjects.Nodes.Add(MeshManObjects);

            ImportedItems = new CreateNewItem(MainForm, "Imported objects", "Various meshes and subtrees imported from other files");
            //todo import VSB, OBJ
            treeObjects.Nodes.Add(ImportedItems);

            LightObjects = new CreateNewItem(MainForm, "Light objects", "Various types of lights");
            LightObjects.Nodes.Add(new CreateLightItem(MainForm));
            treeObjects.Nodes.Add(LightObjects);

            CameraObjects = new CreateNewItem(MainForm, "Camera objects", "Objects for various cameras");
            CameraObjects.Nodes.Add(new CreateCameraItem(MainForm));
            treeObjects.Nodes.Add(CameraObjects);

            HelperObjects = new CreateNewItem(MainForm, "Helper objects", "Miscelaneous helper objects");
            //todo hierarchy dummy, active area
            treeObjects.Nodes.Add(HelperObjects);
        }

        private void afterSelect(object sender, TreeViewEventArgs e)
        {
            CreateNewItem node = e.Node as CreateNewItem;
            if (node == null)
                return;
            textBoxDescription.Text = node.Description;
        }

        private void buttonDefault_Click(object sender, EventArgs e)
        {
            CreateNewItem node = treeObjects.SelectedNode as CreateNewItem;

            if (node == null)
                return;

            node.CreateDefault();
            MainForm.UpdateSceneChange();
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            CreateNewItem node = treeObjects.SelectedNode as CreateNewItem;

            if (node == null)
                return;

            node.ShowDialog();
        }
    }


    public  class CreateNewItem : TreeNode
    {
        public string Description;
        public ObjectCreationBase BaseCreator;
        public MainBuilderForm Boss;
        protected ObjectCreationDialog MyDialog;

        public CreateNewItem(MainBuilderForm boss, string name, string descr) : base(name)
        {
            Boss = boss;
            Name = name;
            Description = descr;
        }

        public override string ToString()
        {
            return Name.ToString();
        }

        public virtual void CreateDefault()
        {
            if (BaseCreator.Boss == null) BaseCreator.Boss = Boss;
            BaseCreator.SetDefault();
            BaseCreator.CreateObject();
        }

        protected virtual void RefreshBaseCreator()
        {
        }

        public virtual void ShowDialog()
        {
            if (BaseCreator == null)
                return;
            if (BaseCreator.Boss == null) BaseCreator.Boss = Boss;
            if (BaseCreator.IsDisposed)
                RefreshBaseCreator();
            if ((MyDialog == null) || (MyDialog.IsDisposed))
            {
                MyDialog = new ObjectCreationDialog(BaseCreator);
            }

            MyDialog.Show();
        }
    }

    public class CreateNewItemToolStrip : ToolStripMenuItem
    {
        protected CreateNewItem NewItem;

        public CreateNewItemToolStrip(string name, CreateNewItem newit) : base(name)
        {
            NewItem = newit;
        }

        protected override void OnClick(EventArgs e)
        {
            if (NewItem.BaseCreator == null)
            {
                base.OnClick(e);
                return;
            }
            NewItem.ShowDialog();
        }
    }

}
