using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FakturyWeb.Business
{
  /// <summary>
  /// Jednoduchý Parser
  /// </summary>
  /// <remarks></remarks>
  public partial class ParserFakturaCislo
  {
    private string code;

    public List<Symbol> logic = new List<Symbol>();

    public ParserFakturaCislo(string code)
    {
      this.code = code;
    }

    #region Parsing
    public void Parse()
    {
      bool parsingKeySymb = false;

      string symb = "";
      Symbol logSymb;

      foreach (var z in code.ToArray())
      {
        if (Conversions.ToString(z) == "(")
        {
          if (parsingKeySymb)
            throw new Exception("Chyba parser");
          if (!string.IsNullOrWhiteSpace(symb))
          {
            logSymb = new Symbol(symb); // text
            logic.Add(logSymb);
          }
          symb = "";
          parsingKeySymb = true;
        }

        symb += Conversions.ToString(z);

        if (Conversions.ToString(z) == ")" & parsingKeySymb)
        {
          parsingKeySymb = false;
          logSymb = FindSymb(symb);
          if (logSymb is null)
            throw new Exception("Chyba parser");
          logic.Add(logSymb);
          symb = "";
        }
      }

      // nokenc ještě:
      if (!string.IsNullOrWhiteSpace(symb))
      {
        logSymb = new Symbol(symb); // text
        logic.Add(logSymb);
      }
    }
    #endregion

    private Symbol FindSymb(string symb)
    {
      switch (symb ?? "")
      {
        case "(CISLO2)":
          {
            return new Symbol(SymbolEnum.CISLO2);
          }
        case "(CISLO4)":
          {
            return new Symbol(SymbolEnum.CISLO4);
          }
        case "(CISLO6)":
          {
            return new Symbol(SymbolEnum.CISLO6);
          }
        case "(MESIC)":
          {
            return new Symbol(SymbolEnum.MESIC);
          }
        case "(ROK)":
          {
            return new Symbol(SymbolEnum.ROK);
          }

        default:
          {
            return null;
          }
      }
    }

  }

  public partial class Symbol
  {

    private string text;

    private SymbolEnum typ;

    public SymbolEnum GetTyp
    {
      get
      {
        return typ;
      }
    }

    public Symbol(SymbolEnum typ)
    {
      this.typ = typ;
    }

    public Symbol(string text)
    {
      typ = SymbolEnum.TEXT;
      this.text = text;
    }

    public override string ToString()
    {
      switch (typ)
      {
        case SymbolEnum.TEXT:
          {
            return text;
          }

        default:
          {
            return typ.ToString();
          }
      }
    }
  }

  public enum SymbolEnum
  {
    CISLO2,
    CISLO4,
    CISLO6,
    MESIC,
    ROK,
    TEXT
  }
}
