using System;
using System.Collections.Generic;

namespace Sonex.Client.Data
{
    public sealed class DataColumnString : DataColumnBase
    {
        public List<string?> Values { get; } = new();

        public override Type DataType => typeof(string);
        public override int RowCount => Values.Count;

        public DataColumnString(string key, string header, int initialRowCount = 0)
            : base(key, header)
        {
            for (int i = 0; i < initialRowCount; i++)
                Values.Add(null);
        }

        protected override string? FormatValue(int rowIndex)
        {
            return Values[rowIndex];
        }

        public override void SetValue(int rowIndex, string? value)
        {
            ValidateRowIndex(rowIndex);
            Values[rowIndex] = value;
        }

        public override object? GetRawValue(int rowIndex)
        {
            ValidateRowIndex(rowIndex);
            return Values[rowIndex];
        }

        public override void SetRawValue(int rowIndex, object? value)
        {
            ValidateRowIndex(rowIndex);

            if (value is null || value is string)
            {
                Values[rowIndex] = (string?)value;
                return;
            }

            throw new InvalidCastException($"Column '{Key}' expects string.");
        }

        public override void AddEmpty()
        {
            Values.Add(null);
        }
    }
}