using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Text;
using Windows.Storage;
using Windows.Storage.Pickers;
using WinRT.Interop;
using System.Runtime.CompilerServices;

namespace HelloXaml.Models
{
    internal class DraftEntry: INotifyPropertyChanged
    {
        public static string BASE_PATH = "../img/drafts/";
        public static volatile string DRAFT_EXTENSION = ".tit";

        private string _coverIcon; //Cover icon path
        private string _title; //Draft title
        private string _description;
        private string _credits; //Draft credits
        private string _category; //Draft category

        public string GetDraftFileName() {
            //Strip name from cover icon path
            return _coverIcon.Split('.')[0];
        }

        public async void SaveFile(ObservableCollection<FileEntry> fileEntries, MainWindow window)
        {
            FileSavePicker savePicker = new FileSavePicker();

            IntPtr hWnd = WindowNative.GetWindowHandle(window);
            InitializeWithWindow.Initialize(savePicker, hWnd);

            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.FileTypeChoices.Add("Draft File", new List<string>() { DRAFT_EXTENSION });
            savePicker.SuggestedFileName = GetDraftFileName();

            StorageFile file = await savePicker.PickSaveFileAsync();

            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                await FileIO.WriteTextAsync(file, toString(fileEntries));
                await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }

        public string CoverIcon {
            get {
                return _coverIcon;
            }
            
            set {
                if (value != _coverIcon) {
                    _coverIcon = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Title {
            get {
                return _title;
            }
            
            set {
                if (value != _title) {
                    _title = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Description
        {
             get {
                 return _description;
             }
            
             set {
                 if (value != _description) {
                     _description = value;
                     OnPropertyChanged();
                 }
             }

        }

        public string Credits {
            get {
                return _credits;
            }
            
            set {
                if (value != _credits) {
                    _credits = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Category {
            get {
                return _category;
            }
            
            set {
                if (value != _category) {
                    _category = value;
                    OnPropertyChanged();
                }
            }
        }

        public DraftEntry() {
            _coverIcon = "-";
            _title = "-";
            _description = "-";
            _credits = "-";
            _category = "-";

            OnPropertyChanged();
        }

        public string toString(ObservableCollection<FileEntry> fileEntries)
        {
            string result = BASE_PATH + _coverIcon + "\n" + _title + "\n" + _description + "\n" + _credits + "\n" + _category;


            result += "\n"+BASE_PATH + _coverIcon;
            foreach (var file in fileEntries)
            {
                //Console.WriteLine("File: " + file.Path + ", NeedsToBeSaved: " + file.NeedsToBeSaved);
                if (file.NeedsToBeSaved)
                {
                    result += "\n"+BASE_PATH + file.Path;
                }
            }
            return result;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
