using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace _1604523ListViewCustomCells
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
            this.radListView1.CellCreating += radListView1_CellCreating;
            this.radListView1.ViewType = ListViewType.DetailsView;
        }

        private void radListView1_CellCreating(object sender, ListViewCellElementCreatingEventArgs e)
        {
            DetailListViewDataCellElement cell = e.CellElement as DetailListViewDataCellElement;
            if (cell != null)
            {
                if (cell.Data.Name == "Picture")
                {
                    e.CellElement = new CustomDetailListViewDataCellElement(cell.RowElement, e.CellElement.Data);
                }
                else
                {
                    e.CellElement = new DefaultDetailListViewDataCellElement(cell.RowElement, e.CellElement.Data);
                }
            }
        }

        private void RadForm1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nwindDataSet.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.nwindDataSet.Products);

            this.radListView1.DataSource = this.productsBindingSource;
        }

        public class DefaultDetailListViewDataCellElement : DetailListViewDataCellElement
        {
            public DefaultDetailListViewDataCellElement(DetailListViewVisualItem owner,
                ListViewDetailColumn column) : base(owner, column)
            {
            }

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

            public override bool IsCompatible(ListViewDetailColumn data, object context)
            {
                if (data.Name == "Picture")
                {
                    return false;
                }
                return base.IsCompatible(data, context);
            }
        }
        
        public class CustomDetailListViewDataCellElement : DetailListViewDataCellElement
        {
            private StackLayoutElement container;
            private LightVisualElement leftImage;
            private LightVisualElement centerImage;
            private LightVisualElement rightImage;

            public CustomDetailListViewDataCellElement(DetailListViewVisualItem owner,
                ListViewDetailColumn column) : base(owner, column)
            {
            }

            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                container = new StackLayoutElement();
                container.Orientation = Orientation.Horizontal;
                this.leftImage = new LightVisualElement();
                this.leftImage.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                this.centerImage = new LightVisualElement();
                this.centerImage.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                this.rightImage = new LightVisualElement();
                this.rightImage.ImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                this.container.Children.Add(leftImage);
                this.container.Children.Add(centerImage);
                this.container.Children.Add(rightImage);
                this.Children.Add(this.container);
            }

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

            public override void Synchronize()
            {
                base.Synchronize();
                this.Text = "";
                DataRowView rowView = this.Row.DataBoundItem as DataRowView;
                if (rowView != null)
                {
                    this.leftImage.Image = GetImageFromData(rowView.Row["Picture"] as byte[]);
                    this.centerImage.Image = GetImageFromData(rowView.Row["Picture"] as byte[]);
                    this.rightImage.Image = GetImageFromData(rowView.Row["Picture"] as byte[]);
                }
            }

            private Image GetImageFromData(byte[] imageData)
            {
                const int OleHeaderLength = 78;
                MemoryStream memoryStream = new MemoryStream();
                if (HasOleContainerHeader(imageData))
                {
                    memoryStream.Write(imageData, OleHeaderLength, imageData.Length - OleHeaderLength);
                }
                else
                {
                    memoryStream.Write(imageData, 0, imageData.Length);
                }
                Bitmap bitmap = new Bitmap(memoryStream);
                return bitmap.GetThumbnailImage(55, 65, null, new IntPtr());
            }

            private bool HasOleContainerHeader(byte[] imageByteArray)
            {
                const byte OleByte0 = 21;
                const byte OleByte1 = 28;
                return (imageByteArray[0] == OleByte0) && (imageByteArray[1] == OleByte1);
            }

            public override bool IsCompatible(ListViewDetailColumn data, object context)
            {
                if (data.Name != "Picture")
                {
                    return false;
                }
                return base.IsCompatible(data, context);
            }
        }
    }
}