using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Globalization;
using C1.Silverlight.FlexGrid;

namespace C1.Silverlight.FlexGridExcel
{
    /// <summary>
    /// GroupRow that appears below the data.
    /// </summary>
    /// <remarks>
    /// <para>When the <see cref="C1FlexGridExcel.ShowBottomGroupRows"/> property is set to 
    /// true, and the <see cref="C1FlexGrid.GroupRowPosition"/> property is set to <b>Above</b>, 
    /// these rows are added below each group to facilitate reading the summary data which is 
    /// already displayed at the regular group row above the groups.</para>
    /// <para>You can control the appearance of these rows using the <see cref="C1FlexGridExcel.BottomGroupRowStyle"/>
    /// property, and the text that appears in the first cell using the <see cref="C1FlexGridExcel.BottomGroupRowCaption"/>
    /// property.</para>
    /// </remarks>
    public class BottomGroupRow : Row
    {
        GroupRow _gr;

        /// <summary>
        /// Initializes a new instance of a <see cref="BottomGroupRow"/>.
        /// </summary>
        public BottomGroupRow(GroupRow gr)
        {
            _gr = gr;
            IsReadOnly = true;
            DataItem = gr.DataItem;
        }
        /// <summary>
        /// Gets the GroupRow that matches this BottomGroupRow.
        /// </summary>
        public GroupRow GroupRow
        {
            get { return _gr; }
        }
        /// <summary>
        /// Gets the group level represented by this BottomGroupRow.
        /// </summary>
        public int Level
        {
            get { return _gr.Level; }
        }
        /// <summary>
        /// Gets the range of cells that this group refers to.
        /// </summary>
        public CellRange GetCellRange()
        {
            // start with base group row
            var rg = _gr.GetCellRange();

            // reduce to exclude any BottomGroupRows
            var rows = _gr.Grid.Rows;
            var bgr = rows[rg.Row2] as BottomGroupRow;
            while (bgr != null && bgr.Level <= this.Level)
            {
                rg.Row2--;
                bgr = rows[rg.Row2] as BottomGroupRow;
            }

            // range is ready
            return rg;
        }
        /// <summary>
        /// Gets the data for a cell in this row.
        /// </summary>
        /// <param name="col">Column to get the data for.</param>
        /// <returns>The data for a cell in this row.</returns>
        protected override object GetData(Column col)
        {
            // default value
            object value = null;

            // override
            var xlg = this.Grid as C1FlexGridExcel;
            if (col.Left == 0)
            {
                // build group header text
                value = xlg.GroupHeaderConverter.Convert(null, typeof(string), this, CultureInfo.CurrentUICulture);
            }
            else if (col.GroupAggregate != Aggregate.None)
            {
                // copy aggregates from actual group row
                value = _gr[col];
            }
            else if (xlg.GetFlatList() != null)
            {
                // leave the cell blank if it has no aggregate
                value = null;
            }

            // return the cell value
            return value;
        }
    }
}
