using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// 발송자 이름 목록을 처리한다.
/// </summary>
public class SenderNameList
{
    public class Item
    {
        public Item(string name)
        {
            Name = name;
        }
        public string Name { get; set; }
    }

    private List<Item> _items = new List<Item>();
    private int _maxCount = 5;

    public SenderNameList(string data, int maxCount = 5)
    {
        string[] names = data.Split(new char[] { '|' });
        foreach (string name in names)
            if (name.Length > 0) _items.Add(new Item(name));
        _maxCount = maxCount;
    }

    public SenderNameList(string data, string append, int maxCount = 5)
    {
        string data2 = data + "|";
        if (data2.IndexOf(append + "|") == -1) data += "|" + append;
        string[] names = data.Split(new char[] { '|' });
        foreach (string name in names)
            if (name.Length > 0) _items.Add(new Item(name));
        _maxCount = maxCount;
    }

    public List<Item> GetDataSource()
    {
        return _items;
    }

    public override string ToString()
    {
        string value = "";
        foreach (Item item in _items)
            value += item.Name + "|";
        int p = value.LastIndexOf('|');
        return (p > 0) ? value.Substring(0, p) : value;
    }

    public void AddName(string name)
    {
        _items.RemoveAll(item => item.Name == name);
        _items.Insert(0, new Item(name));
        if (_items.Count > _maxCount) _items.RemoveAt(_items.Count - 1);
    }

    public void RemoveName(string name)
    {
        _items.RemoveAll(item => item.Name == name);
    }

    public string GetFirstName()
    {
        return (_items.Count > 0) ? _items[0].Name : "";
    }
}