using System;
using System.Collections.Generic;
using System.IO;

namespace FileSystemSimulator
{
    class FileSystemObject
    {
        public string Name { get; set; }
        public string Type { get; set; } // "file" vai "folder"
        public FileSystemObject Parent { get; set; }
        public List<FileSystemObject> Children { get; set; }

        public FileSystemObject(string name, string type)
        {
            Name = name;
            Type = type.ToLower();
            Children = new List<FileSystemObject>();
        }

        public void ShowChildren()
        {
            foreach (var child in Children)
            {
                Console.WriteLine(child.Type == "folder" ? "/" + child.Name : $"~[{child.Name}]~");
            }
        }

        public void AddChild(FileSystemObject child)
        {
            if (Children.Exists(c => c.Name.Equals(child.Name, StringComparison.OrdinalIgnoreCase)))
            {
                Console.WriteLine($"Objekts '{child.Name}' jau eksistē.");
                return;
            }
            child.Parent = this;
            Children.Add(child);
        }

        public bool RemoveChild(string name)
        {
            var child = Children.Find(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            if (child == null)
            {
                Console.WriteLine("Objekts nav atrasts.");
                return false;
            }

            if (child.Type == "folder" && child.Children.Count > 0)
            {
                Console.Write("Mape nav tukša. Vai dzēst? (j/n): ");
                if (Console.ReadLine()?.ToLower() != "j") return false;
            }

            Children.Remove(child);
            return true;
        }

        public FileSystemObject GetChild(string name)
        {
            return Children.Find(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }
    }

    class FileSystem
    {
        private FileSystemObject root;
        private FileSystemObject current;

        public FileSystem()
        {
            root = new FileSystemObject("C:", "folder");
            current = root;
        }

        private string GetPath()
        {
            var pathStack = new Stack<string>();
            var temp = current;
            while (temp != null)
            {
                pathStack.Push(temp.Name);
                temp = temp.Parent;
            }
            return string.Join("/", pathStack);
        }

        public void Run()
        {
            while (true)
            {
                Console.Write($"{GetPath()}> ");
                var input = Console.ReadLine()?.Trim();
                if (string.IsNullOrWhiteSpace(input)) continue;

                var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                var cmd = parts[0].ToLower();

                try
                {
                    switch (cmd)
                    {
                        case "mkdir":
                        case "create":
                            var type = cmd == "mkdir" ? "folder" : "file";
                            for (int i = 1; i < parts.Length; i++)
                            {
                                current.AddChild(new FileSystemObject(parts[i], type));
                            }
                            break;

                        case "dir":
                            current.ShowChildren();
                            break;

                        case "rm":
                            for (int i = 1; i < parts.Length; i++)
                            {
                                current.RemoveChild(parts[i]);
                            }
                            break;

                        case "cd":
                            if (parts.Length != 2)
                            {
                                Console.WriteLine("Izmantošana: cd <mape>, cd .., vai cd /");
                                break;
                            }

                            var target = parts[1];
                            if (target == "..")
                                current = current.Parent ?? current;
                            else if (target == "/")
                                current = root;
                            else
                            {
                                var child = current.GetChild(target);
                                if (child != null && child.Type == "folder")
                                    current = child;
                                else
                                    Console.WriteLine("Mape nav atrasta.");
                            }
                            break;

                        case "edit":
                            if (parts.Length < 2)
                            {
                                Console.WriteLine("Norādi datnes nosaukumu.");
                                break;
                            }

                            var file = current.GetChild(parts[1]);
                            if (file != null && file.Type == "file")
                            {
                                var path = parts[1] + ".txt";
                                Console.WriteLine(File.Exists(path) ? File.ReadAllText(path) : "Fails ir tukšs.");
                            }
                            else
                                Console.WriteLine("Datne nav atrasta.");
                            break;

                        case "del":
                            foreach (var name in parts[1..])
                            {
                                File.Delete(name + ".txt");
                                current.RemoveChild(name);
                                Console.WriteLine("Dzēsts: " + name);
                            }
                            break;

                        case "clear":
                            Console.Clear();
                            break;

                        case "exit":
                        case "by":
                            return;

                        case "help":
                            Console.WriteLine("""
                                Komandas:
                                mkdir <vārds>    - izveidot mapi
                                create <vārds>   - izveidot failu
                                rm <vārds>       - dzēst failu vai mapi
                                dir              - parādīt mapes saturu
                                cd <mape|..|/>   - mainīt direktoriju
                                edit <fails>     - skatīt failu
                                del <fails>      - dzēst failu no diska
                                clear            - notīrīt ekrānu
                                exit/by          - iziet
                                """);
                            break;

                        default:
                            Console.WriteLine("Nezināma komanda. Raksti 'help' pēc palīdzības.");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Kļūda: " + ex.Message);
                }
            }
        }
    }

    class Program
    {
        static void Main()
        {
            var fs = new FileSystem();
            fs.Run();
        }
    }
}
