using System;
using System.Collections.Generic;
using FastReport.Table;

namespace FastReport.AdvMatrix
{
    internal enum CellContentType
    {
        MixedText,
        SingleExpression,
        Aggregate
    }

    internal class CellDescriptor
    {
        public TableCell TemplateCell { get; private set; }
        public List<string> Expressions { get; private set; }
        public AggregateExpressionPairList Aggregates { get; private set; }
        public List<string> SpecialFunctions { get; private set; }
        public string Text { get; set; }
        public int ColumnIndex { get; set; }
        public int RowIndex { get; set; }
        public CellContentType ContentType { get; private set; }
        public bool HasRowTotal { get; private set; }
        public bool HasColumnTotal { get; private set; }
        public bool HasGrandTotal { get; private set; }

        public CellDescriptor(AdvMatrixObject matrix, TableCell templateCell)
        {
            TemplateCell = templateCell;
            Expressions = new List<string>();
            Aggregates = new AggregateExpressionPairList(matrix);
            SpecialFunctions = new List<string>();
            Text = "";
            templateCell.SetDescriptor(this);
        }

        public void ClearData()
        {
            foreach (AggregateExpressionPair ae in Aggregates)
            {
                ae.ClearData();
            }
        }

        public void GetExpressions(List<string> expressions)
        {
            expressions.AddRange(Expressions);
            
            foreach (AggregateExpressionPair ae in Aggregates)
            {
                expressions.Add(ae.Expression);
            }
        }

        public void UpdateContentType()
        {
            foreach (string ident in SpecialFunctions)
            {
                if (ident.Contains("ColumnTotal"))
                    HasColumnTotal = true;
                if (ident.Contains("RowTotal"))
                    HasRowTotal = true;
                if (ident.Contains("GrandTotal"))
                    HasGrandTotal = true;
            }

            if (Expressions.Count == 1)
            {
                if (Aggregates.Count == 1 && Expressions[0] == Aggregates[0].ToAggregateCall())
                {
                    ContentType = CellContentType.Aggregate;
                }
                else
                {
                    ContentType = CellContentType.SingleExpression;
                    Text = Expressions[0];
                }
            }
        }
    }
}