﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

//[assembly: Xamarin.Forms.Dependency(typeof(AvvmTools.UWP.Utils.UwpUtils))]
namespace AvvmTools.UWP.Utils
{
    public class UwpUtils : AvvmTools.Utils
    {
        public UwpUtils()
        {
        }

        public override double GetUtcOffset(DateTime asof)
        {
            var currentTz = TimeZoneInfo.Local;
            var utcOffset = currentTz.GetUtcOffset(asof).TotalHours;
            return utcOffset;
        }

        public override void CopyToClipboard(string id, string value)
        {
            // TODO implement copy to clipboard in UWP
            //var clipboard = UIPasteboard.General;
            //clipboard.String = value;
            //// Just setting value for now. I haven't found yet 
            //// a way to set an id for value on clipboard in iOS
        }

        public override void MakeToast(string msg, AvvmTools.ToastLength toastLength)
        {
            Device.BeginInvokeOnMainThread(() => {
                //Toast.MakeText(Application.Context, msg,
                //    (Android.Widget.ToastLength) toastLength).Show();
            });
        }

        public override void ConsoleWriteLine(string msg)
        {
            Device.BeginInvokeOnMainThread(() => {
                Debug.WriteLine(msg);
            });
        }

        public override string DocumentsPath {
            get { return Windows.Storage.ApplicationData.Current.LocalFolder.Path; }
        }

        public override void SaveToFile(string folderPath, string filename, string contents)
        {
            // TODO: Use PCLCrypto here to encrypt contents
            if (string.IsNullOrEmpty(folderPath))
                folderPath = this.DocumentsPath;
            var filePath = Path.Combine(folderPath, filename);
            File.WriteAllText(filePath, contents);
        }

        public override string GetFromFile(string folderPath, string filename)
        {
            if (string.IsNullOrEmpty(folderPath))
                folderPath = this.DocumentsPath;
            var filePath = Path.Combine(folderPath, filename);
            var contents = File.ReadAllText(filePath);
            // TODO: Use PCLCrypto here to decrypt contents
            return contents;
        }

        public override int BeginBackgroundTask(string taskName, Action expirationHandler)
        {
            return base.BeginBackgroundTask(taskName, expirationHandler);
        }

        public override void EndBackgroundTask(int taskId)
        {
            base.EndBackgroundTask(taskId);
        }
    }
}
