using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BankTVUpdater.Consts;
using BankTVUpdater.Logger;
using BankTVUpdater.Exceptions;
using System.Windows;
using BankTVUpdater.AppUpdaterService;
using System.Xml;
using System.Net;
using BankTVUpdater.Model.Service;

namespace BankTVUpdater.Helpers.Service
{
    class FilesHelper
    {

        public static List<SingleFile> ConvertWSFilesToList(Files files)
        {
            List<SingleFile> result = new List<SingleFile>();
            try
            {
                if (files == null)
                {
                    return result;
                }   //if
                if (files.Any == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in files.Any)
                {
                    try
                    {
                        string fileHost = FilesHelper.GetElementValueByName(item, GlobalConsts.FILE_HOST_TAG_NAME);
                        if (String.IsNullOrEmpty(fileHost))
                        {
                            throw new Exception("Unable to get host of one of the files to download...");
                        }   //if
                        string fileDir = FilesHelper.GetElementValueByName(item, GlobalConsts.FILE_DIR_TAG_NAME);
                        if (String.IsNullOrEmpty(fileDir))
                        {
                            throw new Exception("Unable to get dir of one of the files to download...");
                        }   //if
                        string fileName = FilesHelper.GetElementValueByName(item, GlobalConsts.FILE_NAME_TAG_NAME);
                        if (String.IsNullOrEmpty(fileName))
                        {
                            throw new Exception("Unable to get name of one of the files to download...");
                        }   //if
                        SingleFile singleFile = new SingleFile();
                        singleFile.Checksum = FilesHelper.GetElementValueByName(item, GlobalConsts.FILE_CHECKSUM_TAG_NAME);
                        singleFile.FileHost = fileHost;
                        singleFile.FileDirectory = fileDir;
                        singleFile.FileName = fileName;                        
                        result.Add(singleFile);
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get full file path of one of the files.");                        
                        MyLogger.Log(ex);                        
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        public static List<SingleNews> ConvertWSNewsFilesToList(News news)
        {
            List<SingleNews> result = new List<SingleNews>();
            try
            {
                if (news == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in news.Any)
                {
                    try
                    {
                        string fileHost = FilesHelper.GetElementValueByName(item, GlobalConsts.NEWS_FILE_HOST_TAG_NAME);
                        string fileDir = FilesHelper.GetElementValueByName(item, GlobalConsts.NEWS_FILE_DIR_TAG_NAME);
                        string fileName = FilesHelper.GetElementValueByName(item, GlobalConsts.NEWS_FILE_NAME_TAG_NAME);
                        if (!((String.IsNullOrEmpty(fileHost)) || (String.IsNullOrEmpty(fileDir)) || (String.IsNullOrEmpty(fileName))))
                        {
                            SingleNews singleNews = new SingleNews();
                            singleNews.ImageDirectory = fileDir;
                            singleNews.ImageHost = fileHost;
                            singleNews.ImageName = fileName;
                            result.Add(singleNews);
                        }
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get full news file path of one of the news.");
                        MyLogger.Log(ex);
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        public static List<CurrencyItem> ConvertWSCurrencyItemsToList(CurrencyItemsList currencyItems)
        {
            List<CurrencyItem> result = new List<CurrencyItem>();
            try
            {
                if (currencyItems == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in currencyItems.Any)
                {
                    try
                    {
                        string fileHost = FilesHelper.GetElementValueByName(item, GlobalConsts.CURRENCY_FILE_HOST_TAG_NAME);
                        string fileDir = FilesHelper.GetElementValueByName(item, GlobalConsts.CURRENCY_FILE_DIR_TAG_NAME);
                        string fileName = FilesHelper.GetElementValueByName(item, GlobalConsts.CURRENCY_FILE_NAME_TAG_NAME);
                        if (!((String.IsNullOrEmpty(fileHost)) || (String.IsNullOrEmpty(fileDir)) || (String.IsNullOrEmpty(fileName))))
                        {
                            CurrencyItem currency = new CurrencyItem();
                            currency.ImageDirectory = fileDir;
                            currency.ImageHost = fileHost;
                            currency.ImageName = fileName;
                            result.Add(currency);
                        }
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get full currency file path of one of the currency items.");
                        MyLogger.Log(ex);
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        public static List<WeatherIcon> ConvertWSWeatherIconsToList(WeatherIcons weatherIcons)
        {
            List<WeatherIcon> result = new List<WeatherIcon>();
            try
            {
                if (weatherIcons == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in weatherIcons.Any)
                {
                    try
                    {
                        string fileHost = FilesHelper.GetElementValueByName(item, GlobalConsts.WEATHER_FILE_HOST_TAG_NAME);
                        string fileDir = FilesHelper.GetElementValueByName(item, GlobalConsts.WEATHER_FILE_DIR_TAG_NAME);
                        string fileName = FilesHelper.GetElementValueByName(item, GlobalConsts.WEATHER_FILE_NAME_TAG_NAME);
                        if (!((String.IsNullOrEmpty(fileHost)) || (String.IsNullOrEmpty(fileDir)) || (String.IsNullOrEmpty(fileName))))
                        {
                            WeatherIcon icon = new WeatherIcon();
                            icon.ImageDirectory = fileDir;
                            icon.ImageHost = fileHost;
                            icon.ImageName = fileName;
                            result.Add(icon);
                        }
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get full weather icon file path of one of the weather icons.");
                        MyLogger.Log(ex);
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        private static string GetElementValueByName(XmlElement xml, string itemName)
        {
            try
            {
                if (xml == null)
                {
                    return String.Empty;
                }   //if
                XmlNodeList elements = xml.GetElementsByTagName(itemName);
                if (elements == null)
                {
                    return String.Empty;
                }   //if
                if (elements.Count == 0)
                {
                    return String.Empty;
                }   //if
                XmlNode node = elements[0];
                if (node == null)
                {
                    return String.Empty;
                }   //if
                return node.InnerText;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return String.Empty;
            }   //catch
        }

        private static bool DownloadFile(string url, string destDir, string destFileName, bool overrideFiles = true)
        {
            if (String.IsNullOrEmpty(url))
            {
                return false;
            }   //if
            if (String.IsNullOrEmpty(destDir))
            {
                return false;
            }   //if
            if (String.IsNullOrEmpty(destFileName))
            {
                return false;
            }   //if
            try
            {
                if (!Directory.Exists(destDir))
                {
                    Directory.CreateDirectory(destDir);
                }   //if
                string filePath = Path.Combine(destDir, destFileName);
                if (File.Exists(filePath))
                {
                    if (!overrideFiles)
                    {
                        return true;
                    }   //if
                    File.Delete(filePath);
                }   //if
                WebClient webClient = new WebClient();
                webClient.DownloadFile(url, filePath);
                return true;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadImageFile(SingleFile file, bool overrideFiles = true) 
        {
            if (file == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(file.GetFilePath(), GlobalHelper.GetImagesFolder(), file.FileName, overrideFiles);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadFlashFile(SingleFile file, bool overrideFiles = true) 
        {
            if (file == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(file.GetFilePath(), GlobalHelper.GetFlashFolder(), file.FileName, overrideFiles);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadVideoFile(SingleFile file, bool overrideFiles = true) 
        {
            if (file == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(file.GetFilePath(), GlobalHelper.GetVideosFolder(), file.FileName, overrideFiles);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadNewsFile(SingleNews news, bool overrideFiles = true) 
        {
            if (news == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(news.GetFilePath(), GlobalHelper.GetNewsFolder(), news.ImageName, overrideFiles);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadCurrencyImage(CurrencyItem currencyItem, bool overrideFiles = true) 
        {
            if (currencyItem == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(currencyItem.GetFilePath(), GlobalHelper.GetCurrencyFolder(), currencyItem.ImageName, overrideFiles);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static bool DownloadWeatherIcon(WeatherIcon icon)
        {
            if (icon == null)
            {
                return false;
            }   //if
            try
            {
                return FilesHelper.DownloadFile(icon.GetFilePath(), GlobalHelper.GetWeatherIconsFolder(), icon.ImageName);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return false;
            }   //catch
        }

        public static List<string> ConvertWSBottomBarNewsToList(BankTVUpdater.AppUpdaterService.BottomBarNews news)
        {
            List<string> result = new List<string>();
            try
            {
                if (news == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in news.Any)
                {
                    try
                    {
                        string newsContent = FilesHelper.GetElementValueByName(item, GlobalConsts.BOTTOM_BAR_NEWS_CONTENT_TAG_NAME);
                        if (!String.IsNullOrWhiteSpace(newsContent))
                        {
                            result.Add(newsContent);
                        }   //if
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get full file path of one of the files.");
                        MyLogger.Log(ex);
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        public static List<Department> ConvertWSDepartmentsToList(Departments departments)
        {
            List<Department> result = new List<Department>();
            try
            {
                if (departments == null)
                {
                    return result;
                }   //if
                foreach (XmlElement item in departments.Any)
                {
                    try
                    {
                        string parameter = FilesHelper.GetElementValueByName(item, GlobalConsts.DEPARTMENT_ABBR_TAG_NAME);
                        string address = FilesHelper.GetElementValueByName(item, GlobalConsts.DEPARTMENT_ADDRESS_TAG_NAME);
                        string city = FilesHelper.GetElementValueByName(item, GlobalConsts.DEPARTMENT_CITY_TAG_NAME);
                        string id = FilesHelper.GetElementValueByName(item, GlobalConsts.DEPARTMENT_ID_TAG_NAME);
                        string name = FilesHelper.GetElementValueByName(item, GlobalConsts.DEPARTMENT_NAME_TAG_NAME);
                        if (!((String.IsNullOrEmpty(parameter)) || (String.IsNullOrEmpty(id)) || (String.IsNullOrEmpty(name)) || (String.IsNullOrEmpty(address)) || (String.IsNullOrEmpty(city))))
                        {
                            Department department = new Department();
                            department.Address = address;
                            department.City = city;
                            department.Id = id;
                            department.Name = name;
                            department.Parameter = parameter;
                            result.Add(department);
                        }
                    }   //try
                    catch (Exception ex)
                    {
                        MyLogger.Log("Unable to get departments...");
                        MyLogger.Log(ex);
                    }   //catch            
                }   //for
                return result;
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch            
        }

    }

}
