using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace PDF_Office.DataConvert
{
    public class DateTimeToStringConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            else
            {
                if (value is DateTime)
                {
                    var datetime = (DateTime)value;
                    return datetime.Year + "-" + datetime.Month +"-"+ datetime.Day + "  " + datetime.ToString("HH:mm:ss");
                }
                return null;

            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class FilePathToSizeConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            else
            {
                if (value is string)
                {
                   return GetFileSize((string)value);
                }
                return null;

            }
        }

        public string GetFileSize(string path)
        {
            System.IO.FileInfo fileInfo = null;
            try
            {
                fileInfo = new System.IO.FileInfo(path);
            }
            catch
            {
                return "0KB";
            }
            if (fileInfo != null && fileInfo.Exists)
            {
               
                var size = Math.Round(fileInfo.Length / 1024.0, 0);
                if (size > 1024)
                {
                   var sizeDouble = Math.Round(size / 1024.0,2);
                    if(sizeDouble > 1024)
                    {
                        sizeDouble = Math.Round(sizeDouble / 1024.0,2);
                        return sizeDouble + "G";
                    }
                    else
                    return sizeDouble + "M";
                }
                else
                {
                    
                    return (int)System.Math.Ceiling(size) + "KB";
                }
               
            }
            else
            {
                return "0KB";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}