// GET: TextParser
        public ActionResult Index()
        {
            TextParserViewModel loTextParserViewModel = new TextParserViewModel();

            ViewBag.directoryFiles = Directory.GetFiles(Server.MapPath(Constants.FilePath));
            return(View());
        }
        public ActionResult Index(TextParserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                #region Save File
                HttpPostedFileBase postedFile = viewModel.file;

                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(postedFile.FileName);
                    string path     = Path.Combine(Server.MapPath(Constants.FilePath), fileName);
                    int    count    = 1;

                    while (System.IO.File.Exists(path))
                    {
                        string tempFileName = string.Format("{0}({1})", Path.GetFileNameWithoutExtension(postedFile.FileName), count++);
                        path = Path.Combine(Server.MapPath(Constants.FilePath), tempFileName + Path.GetExtension(postedFile.FileName));
                    }
                    postedFile.SaveAs(path);

                    TempData["Success"] = "File " + Path.GetFileName(path) + " uploaded successfully!";
                }
                #endregion
            }
            ViewBag.directoryFiles = Directory.GetFiles(Server.MapPath(Constants.FilePath));
            return(View(viewModel));
        }
        public ActionResult Parse(TextParserViewModel model)
        {
            List <BalanceItem> parsed = new List <BalanceItem>();

            //BalanceItem exi = null;

            //try
            //{
            //    StringBuilder sb = new StringBuilder();
            //    string[] lines = model == null ? null : model.Text.Split(Environment.NewLine.ToCharArray());
            //    int temp = 0;
            //    decimal tempPrice = 0m;
            //    string codeAndName = "";
            //    int pos = 0;

            //    for(int i = 0; (lines != null) && (i < lines.Length); i++)
            //    {
            //        string line = lines[i].Trim();
            //        string lineNr = (i + i).ToString();
            //        if(line.Length < 5)
            //        {
            //            //ModelState.AddModelError("Text", MyMessages.Parser.LineIsTooShortFmt.Replace("%NR%", lineNr).Replace("%LENGTH%", line.Length.ToString()));
            //        } else
            //        {
            //            string[] ar = line.Split(new char[] { '\t' });
            //            if(ar.Length < 4)
            //            {
            //                ModelState.AddModelError("Text", MyMessages.Parser.SplittedLineHasTooLessArgumentsFmt.Replace("%NR%", lineNr));
            //            } else
            //            {
            //                exi = new BalanceItem();
            //                codeAndName = ar[0].Trim();
            //                exi.Quantity = int.TryParse(ar[2].Trim(), out temp) ? temp : -1;
            //                exi.Price = decimal.TryParse(ar[3].Trim(), System.Globalization.NumberStyles.Currency, CultureInfo.InvariantCulture, out tempPrice) ? tempPrice : -1;
            //                exi.PriceOfRelease = decimal.TryParse(ar[ar.Length - 1].Trim(), NumberStyles.Currency, CultureInfo.InvariantCulture, out tempPrice) ? tempPrice : -1;

            //                // Search for N-digit code
            //                pos = codeAndName.LastIndexOf(' ');
            //                if(pos >= 0)
            //                {
            //                    exi.InternalCode = "---";
            //                    if(int.TryParse(codeAndName.Substring(pos), out temp))
            //                    {
            //                        exi.InternalCode = temp.ToString();
            //                        exi.ProductName = codeAndName.Substring(0, codeAndName.Length - temp.ToString().Length);
            //                    }
            //                    sb.AppendFormat("Code: {0}, name: {1}, qnt: {2}, price: {3}, price of release: {4}", exi.InternalCode, exi.ProductName, exi.Quantity, exi.Price, exi.PriceOfRelease);

            //                    if( !string.IsNullOrEmpty(exi.ProductName) && !string.IsNullOrEmpty(exi.InternalCode) )
            //                    {
            //                        parsed.Add(exi);
            //                    }
            //                } else
            //                {
            //                    ModelState.AddModelError("Text", MyMessages.Parser.CouldNotParseCodeForLineFmt.Replace("%NR%", lineNr));
            //                }

            //            }
            //        }
            //    }

            //    if( parsed.Count > 0 )
            //    {
            //        TempData["ExchangeItems"] = parsed;
            //        if(Request.Params.AllKeys.Contains("ExportBtn"))
            //        {
            //            return RedirectToAction("export", new { id = 100001 });
            //        }
            //        return RedirectToAction("balance", new { id = 100001 });
            //    }

            //    return Content(sb.ToString());

            //} catch(Exception ex)
            //{
            //    return Content("Error: " + ex.ToString());
            //}

            return(View(model));
        }