﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrioManager
{
    [Serializable]
    /// <summary>
    /// This Class represents a Programm to manage the System Priority
    /// </summary>
    internal class Prog
    {
        /// <summary>
        /// Full App Path
        /// </summary>
        public string FullPath { get; set; }

        /// <summary>
        /// Name from the App without extension
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Array of found Processes by AppName
        /// </summary>
        [NonSerialized]
        private Process[] _Processes;

        /// <summary>
        /// The set Prio for this Application
        /// </summary>
        public ProcessPriorityClass Priority { get; set; }

        /// <summary>
        /// Instaciates a new Prog Object
        /// </summary>
        /// <param name="fullPath">Full path of given Application</param>
        public Prog(string fullPath)
        {
            this.FullPath = FullPath;
            this.Name = GetNameFromPath(FullPath);
            this.Priority = ProcessPriorityClass.Normal;
            this.IsRunning = false;
        }

        /// <summary>
        /// True if Process is up and running, False if not so
        /// </summary>
        public bool IsRunning { get; set; }

        /// <summary>
        /// Gets the App Name from the full proper Path
        /// </summary>
        /// <param name="p">Path</param>
        /// <returns>Returns only the Appname without extension</returns>
        private string GetNameFromPath(string p)
        {
            string[] splitPath = p.Split('\\');
            string name = splitPath[splitPath.Length - 1].Split('.')[0];
            return name;
        }

        /// <summary>
        /// Gets the Processes by Name
        /// </summary>
        /// <returns>True if Processes were found</returns>
        public void GetProcess()
        {
            this._Processes = Process.GetProcessesByName(this.Name);
            if (this._Processes.Count() > 0)
            {
                this.IsRunning = true;
            }
            this.IsRunning = false;
        }

        /// <summary>
        /// Sets the Priority of this represented application
        /// </summary>
        /// <param name="prio">Choosen Priority</param>
        public void SetPriority(ProcessPriorityClass prio)
        {
            this.Priority = prio;
        }
    }
}