using FizzyLauncher.Text.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text.Json.Serialization;

namespace FizzyLauncher.Models
{
    public class TileModel
    {
        public string Title { get; set; }

        public string ProcessFilename { get; set; }

        public string ProcessArgument { get; set; }

        public string ProcessWorkingDirectory { get; set; }

        public ProcessWindowStyle ProcessWindowStyle { get; set; } = ProcessWindowStyle.Normal;

        public bool ProcessAsAdmin { get; set; } = false;

        [JsonConverter(typeof(JsonPointConverter))]
        public Point Position { get; set; }

        public bool IsGroup { get; set; } = false;

        public List<TileModel> Items { get; set; } = new List<TileModel>();

        public override string ToString() => this.Title ?? string.Empty;

        public string CleanProcessFilename => (string.IsNullOrWhiteSpace(this.ProcessFilename) ? string.Empty : resolvePath(this.ProcessFilename));

        public string CleanProcessArgument => (string.IsNullOrWhiteSpace(this.ProcessArgument) ? string.Empty : resolvePath(this.ProcessArgument));

        public string CleanProcessWorkingDirectory => (string.IsNullOrWhiteSpace(this.ProcessWorkingDirectory) ? string.Empty : resolvePath(this.ProcessWorkingDirectory));

        protected string resolvePath(string value)
        {
            string rv = Environment.ExpandEnvironmentVariables(value);

            rv = resolveFirstPath(rv);
            rv = resolveLastPath(rv);

            return rv;
        }

        protected string resolveFirstPath(string value)
        {
            const string last = "%FIRST%";
            if (!value.Contains(last))
            {
                return value;
            }

            string head = value.Substring(0, value.IndexOf(last));
            string tail = value.Substring(value.IndexOf(last) + last.Length);

            string[] dirList = new string[0];

            try
            {
                dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly);
            }
            catch
            {
                // do nothing
            }

            if (dirList.Length <= 0)
            {
                return value;
            }

            return dirList[0] + tail;
        }

        protected string resolveLastPath(string value)
        {
            const string last = "%LAST%";
            if (!value.Contains(last))
            {
                return value;
            }

            string head = value.Substring(0, value.IndexOf(last));
            string tail = value.Substring(value.IndexOf(last) + last.Length);

            string[] dirList = new string[0];

            try
            {
                dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly);
            }
            catch
            {
                // do nothing
            }

            if (dirList.Length <= 0)
            {
                return value;
            }

            return dirList[(dirList.Length - 1)] + tail;
        }

    }
}