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

namespace module
{
    class Module
    {
        static void Main(string[] args)
        {
            try
            {
                if (args != null && args.Length > 0)
                {
                    if (args[0] != "exit")
                    {
                        if (args[0].StartsWith("-"))
                            ProcessOptions(args);
                        else
                            ProcessArgs(args);
                    }
                }
                else
                {
                    DisplayHelpString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("plug module: " + ex.Message);
            }
        }

        static void DisplayHelpString()
        {
            string helpString = "usage: plug module [<app name>\\]<module name> /j|/x";

            Console.WriteLine(helpString);
        }

        static void ProcessArgs(params string[] args)
        {
            if (args.Length == 2)
            {
                ConfigFormat cf;
                if (args[1].Equals("/x", StringComparison.CurrentCultureIgnoreCase))
                    cf = ConfigFormat.XML;
                else if (args[1].Equals("/j", StringComparison.CurrentCultureIgnoreCase))
                    cf = ConfigFormat.JSON;
                else
                    throw new ArgumentException(string.Format("Invalid config format '{0}'. Expected '/j' or '/x'", args[1]));

                string name = args[0];
                if (name.Contains('\\'))
                {
                    string[] nameParts = name.Split('\\');
                    if (nameParts.Length == 2)
                    {
                        DirSetup.CreateAppDirStruct(nameParts[0], cf);
                        DirSetup.CreateModuleDirStruct(nameParts[1], cf);
                    }
                    else
                    {
                        DisplayHelpString();
                        return;
                    }
                }
                else
                {
                    DirSetup.CreateModuleDirStruct(name, cf);
                }
            }
            else
            {
                throw new ArgumentException("Invalid format. Expected: plug module [<app name>\\]<module name> /j|/x");
            }
        }
        static void ProcessOptions(params string[] args)
        {
            if (args[0] == "--help" || args[0] == "-h")
                ProcessHelpOptions(args);
            else
            {
                Console.WriteLine("plug module: unknown option: " + args[0]);
                DisplayHelpString();
            }
        }
        static void ProcessHelpOptions(params string[] args)
        {
            Console.WriteLine("Launching default browser to display HTML ...");

            string helpDir = Env.GetHelpDirectory();
            if (!string.IsNullOrWhiteSpace(helpDir))
            {
                string filePath = helpDir + "\\module.html";
                if (System.IO.File.Exists(filePath))
                {
                    System.Diagnostics.Process.Start(filePath);
                }
                else
                {
                    Console.WriteLine("plug module: no help file found");
                }
            }
        }
    }
}
