using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.UI;

namespace TelerikListViewItemChangeInIconView
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            
            this.radListView1.ListViewElement.ViewElement.Orientation = Orientation.Horizontal;

            List<Junkdata> list = new();
            for (int i = 4; i > 0; i--)
                list.Add(new Junkdata()
                {
                    Id = i
                }
            );
            radListView1.DataSource = list;

        }

        private void RadListView1_VisualItemCreating(object sender, ListViewVisualItemCreatingEventArgs e)
        {
            e.VisualItem = new DstVisualItem();
        }

        private void radListView1_SelectedItemChanged(object sender, EventArgs e)
        {
            if (sender is not RadListViewElement element) return;
            if (element.SelectedItem == null) return;
            if (element.SelectedItem.Value is not Junkdata item) return;
            
            this.Text = "Selected ID: " + item.Id;
        }
    }

    public class Junkdata
    {
        public int Id { get; set; } 
    }
    public class DstVisualItem : IconListViewVisualItem
    {
        public int? Id { get; set; }

        private LightVisualElement imageElement;
        private StackLayoutPanel stackLayoutH;

        protected override void CreateChildElements()
        {
            base.CreateChildElements();

            stackLayoutH = new StackLayoutPanel();
            stackLayoutH.Orientation = System.Windows.Forms.Orientation.Horizontal;

            imageElement = new LightVisualElement();
            imageElement.DrawText = false;
            imageElement.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            imageElement.MaxSize = new Size(100, 100);
            imageElement.MinSize = new Size(100, 100);
            imageElement.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            imageElement.Name = "image";

            stackLayoutH.Children.Add(imageElement);

            this.Children.Add(stackLayoutH);

            this.Padding = new Padding(5);
            this.Shape = new RoundRectShape(3);
            this.BorderColor = Color.FromArgb(255, 110, 153, 210);
            this.BorderGradientStyle = GradientStyles.Solid;
            this.DrawBorder = true;
            this.DrawFill = true;
            this.BackColor = Color.FromArgb(255, 230, 238, 254);
            this.GradientStyle = GradientStyles.Solid;
        }


        protected override void SynchronizeProperties()
        {
            this.imageElement.Image = Properties.Resources.smiley;

            Id = (int?)this.Data["Id"];
        }

        protected override SizeF MeasureOverride(SizeF availableSize)
        {
            SizeF measuredSize = base.MeasureOverride(availableSize);

            this.stackLayoutH.Measure(measuredSize);

            return measuredSize;
        }

        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            base.ArrangeOverride(finalSize);

            this.stackLayoutH.Arrange(new RectangleF(PointF.Empty, finalSize));

            return finalSize;
        }

        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(IconListViewVisualItem);
            }
        }
    }
}
