using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BankTV.Consts;
using BankTV.Logger;
using BankTV.Exceptions;
using System.Windows;
using System.Xml;
using System.Net;
using BankTV.BottomBarNews;
using System.Xml.Serialization;

namespace BankTV.Helpers.BottomBarNews
{
    class BottomBarNewsXmlHelper
    {

        private static bottom_bar_news GetBottomBarNewsObject()
        {
            try 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(bottom_bar_news));
                string bottomBarNewsFolder = GlobalHelper.GetBottomBarNewsFolder();
                if (String.IsNullOrEmpty(bottomBarNewsFolder))
                {
                    throw new AppNotInitiatedException("Unable to get bottom bar news folder.");
                }   //if
                string bottomBarNewsFile = System.IO.Path.Combine(bottomBarNewsFolder, GlobalConsts.BOTTOM_BAR_NEWS_FILE);
                if (!File.Exists(bottomBarNewsFile))
                {
                    throw new AppNotInitiatedException("Unable to get bottom bar news file.");
                }   //if
                TextReader reader = new StreamReader(bottomBarNewsFile);
                try
                {
                    return (bottom_bar_news)serializer.Deserialize(reader);
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static List<string> GetBottomBarNewsValues()
        {
            try
            {
                bottom_bar_news bottomBarNewsObject = BottomBarNewsXmlHelper.GetBottomBarNewsObject();
                List<string> news = new List<string>();
                if (bottomBarNewsObject == null)
                {
                    return news;
                }   //if
                foreach (bottom_bar_newsNews_item newsItem in bottomBarNewsObject.Items)
                {
                    if (!String.IsNullOrWhiteSpace(newsItem.content))
                    {
                        news.Add(newsItem.content);
                    }   //if
                }   //for
                return news;
            }
            catch (AppNotInitiatedException)
            {
                return new List<string>();
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                throw;
            }
        }

    }

}
