using System;
using System.Windows;
 
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using System.Reflection;
using System.Windows.Forms.Integration;
using System.Windows.Forms;
 
namespace Bushman.AcPalette {
 
    class TestControl : System.Windows.Controls.ItemsControl,
        IDisposable {
 
        System.Windows.Controls.Label label = new System.Windows
            .Controls.Label();
 
        System.Windows.Controls.TextBox tb = new System.Windows
            .Controls.TextBox();
 
        Document doc;
 
        public TestControl() : base() {
 
            doc = cad.DocumentManager?.MdiActiveDocument;
            tb.Text = doc?.Name;
 
            cad.DocumentManager.DocumentBecameCurrent +=
                DocMng_DocumentBecameCurrent;
 
            label.Content = "Current document:";
            this.Items.Add(label);
            
            tb.Margin = new Thickness(3, 1, 3, 1);
            tb.IsReadOnly = true;
            this.Items.Add(tb);
        }
 
        private void DocMng_DocumentBecameCurrent(object sender,
            DocumentCollectionEventArgs e) {
            doc = e.Document;
            tb.Text = doc?.Name;
        }
 
        void IDisposable.Dispose() {
            cad.DocumentManager.DocumentBecameCurrent -=
                DocMng_DocumentBecameCurrent;
        }
    }
 
    public class ExtensionApplication : IExtensionApplication {
        void IExtensionApplication.Initialize() {
            Document doc = cad.DocumentManager.MdiActiveDocument
                ;
            Editor ed = doc?.Editor;
            string name = Assembly.GetExecutingAssembly()
                .GetName().Name;
            ed?.WriteMessage($"\n'{name}' loaded...\n");
        }
 
        void IExtensionApplication.Terminate() {
            // throw new NotImplementedException();
        }
 
        /// <summary>
        /// This command displays the "Current Document Info" 
        /// palette.
        /// </summary>
        [CommandMethod("test", CommandFlags.Session)]
        public void Test() {
            // Create the new palette set
            PaletteSet ps = new PaletteSet(
                    "Current Document Info");
 
            ElementHost host = new ElementHost();
            host.AutoSize = true;
            host.Dock = DockStyle.Fill;
 
            // Host my WPF control
            TestControl control = new TestControl();
 
            host.Child = control;
            // Create the new tab in the palette set
            Palette palette = ps.Add("test palette", host);
 
            /***************************************************
             * WARNING! 
             * The sequence of the values assignment for the 
             * properties influences behavior of Palette! It is 
             * necessary to set up properties in the order 
             * provided below:
             * 
             * 1. Set the icon */
 
            /* ps.Icon = Icon.FromHandle(Resource1.SheetSet
                .GetHicon()); */
 
            // Display the palette set.
 
            /* 2. The palette shall be visible before setting 
             * the side of the Docking or Docking status. */
            ps.KeepFocus = true;
            ps.Visible = true;
            ps.Style = PaletteSetStyles.ShowCloseButton |
                    PaletteSetStyles.ShowPropertiesMenu |
                    PaletteSetStyles.ShowAutoHideButton;
 
            /* Let the palette set can be docked only to left\
             * right by the user.
             
             * 3. The side (sides) of the Docking must to be 
             * specified before changing of the Docking state. 
             */
            ps.DockEnabled = DockSides.Left | DockSides.Right;
 
            /* The Docking status at the starting time of 
             * displaying of the palette.
             * 
             * 4. Set the Docking status. */
            ps.Dock = DockSides.None;
 
            /* Position of the top left corner of palette */
            ps.Location = new System.Drawing.Point(120, 150);
 
            ps.Size = new System.Drawing.Size(400, 500);
            ps.TitleBarLocation = PaletteSetTitleBarLocation
                .Left;
            ps.Opacity = 100;
 
            /* 5. Don't forget to do it at the end */
            ps.RecalculateDockSiteLayout();
 
            /* P.S. Also you can reed this (russian text): 
            http://adn-cis.org/forum/index.php?topic=364.0
 
            ***************************************************/
        }
    }
}