using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.ViewInfo;

namespace PXP.Crawler.API.Designers.Controls
{
    public class TreeListEx : TreeList
    {
        public TreeListEx() : base() { }

        public event CalcNodeHeightEventHandler CalcSelectImageWidth;

        private int OnCalcSelectImageWidth(TreeListNode node, int defHeight)
        {
            if (CalcSelectImageWidth != null)
            {
                CalcNodeHeightEventArgs e = new CalcNodeHeightEventArgs(node, defHeight);
                CalcSelectImageWidth(this, e);
                return e.NodeHeight;
            }

            return defHeight;
        }

        public event CalcNodeHeightEventHandler CalcStateImageWidth;

        private int OnCalcStateImageWidth(TreeListNode node, int defHeight)
        {
            if (CalcStateImageWidth != null)
            {
                CalcNodeHeightEventArgs e = new CalcNodeHeightEventArgs(node, defHeight);
                CalcStateImageWidth(this, e);
                return e.NodeHeight;
            }

            return defHeight;
        }

        protected override TreeListViewInfo CreateViewInfo() {
            return new TreeListExViewInfo(this);
        }

        public class TreeListExViewInfo : TreeListViewInfo
        {
            public TreeListExViewInfo(TreeList treeList) : base(treeList) { }

            protected override Point GetDataBoundsLocation(TreeListNode node, int top)
            {
                Point result = base.GetDataBoundsLocation(node, top);
                if (Size.Empty != RC.SelectImageSize && -1 == node.SelectImageIndex)
                    result.X -= RC.SelectImageSize.Width;
                if (Size.Empty != RC.StateImageSize && -1 == node.StateImageIndex)
                    result.X -= RC.StateImageSize.Width;


                return result;
            }

            protected override void CalcStateImage(RowInfo ri)
            {
                base.CalcStateImage(ri);



                if (Size.Empty != RC.SelectImageSize && -1 == ri.Node.SelectImageIndex)
                {
                    ri.StateImageLocation.X -= RC.SelectImageSize.Width;                    
                }


                ri.StateImageLocation.X += (RC.StateImageSize.Width - ((TreeListEx)TreeList).OnCalcStateImageWidth(ri.Node, ri.SelectImageBounds.Width));
            }

            protected override void CalcSelectImage(RowInfo ri)
            {
                base.CalcSelectImage(ri);
                if (-1 == ri.Node.SelectImageIndex) ri.SelectImageLocation = Point.Empty;

                //ri.SelectImageBounds.Width = ((TreeListEx)TreeList).OnCalcSelectImageWidth(ri.Node, ri.SelectImageBounds.Width);

                ri.SelectImageLocation.X += (RC.SelectImageSize.Width - ((TreeListEx)TreeList).OnCalcSelectImageWidth(ri.Node, ri.SelectImageBounds.Width));
            }
        }
    }
}
