using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace TreeView.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
        [HttpGet]
        public IActionResult LoadTreeView()
        {
            return PartialView("TreeViewPartial");
        }


        public JsonResult GetSampleData()
        {
            List<CategoryItem> sampleData = new List<CategoryItem>
                {
                    new CategoryItem
                    {
                        CategoryId = 1,
                        CategoryName = "Storage",
                        SubCategories = new List<SubCategoryItem>
                        {
                            new SubCategoryItem()
                            {
                                SubCategoryId = 3,
                                SubCategoryName = "Wall Shelving"
                            },
                            new SubCategoryItem
                            {
                                SubCategoryId = 4,
                                SubCategoryName = "Floor Shelving"
                            },
                            new SubCategoryItem
                            {
                                SubCategoryId = 5,
                                SubCategoryName = "Kids Storage"
                            }
                        }
                    },
                    new CategoryItem
                    {
                        CategoryId = 2,
                        CategoryName = "Lights",
                        SubCategories = new List<SubCategoryItem>
                        {
                            new SubCategoryItem()
                            {
                                SubCategoryId = 6,
                                SubCategoryName = "Ceiling"
                            },
                            new SubCategoryItem
                            {
                                SubCategoryId = 7,
                                SubCategoryName = "Table"
                            },
                            new SubCategoryItem
                            {
                                SubCategoryId = 8,
                                SubCategoryName = "Floor"
                            }
                        }
                    },
                    new CategoryItem
                    {
                        CategoryId = 9,
                        CategoryName = "Sofas",
                        SubCategories = new List<SubCategoryItem>()
                    }
                };

            var outputData = sampleData.Select(item => new
                {
                    id = item.CategoryId,
                    Name = item.CategoryName,
                    HasChildren = item.SubCategories.Any(),
                    Children = item.SubCategories.Select(sc=> new
                    {
                        id = sc.SubCategoryId,
                        Name = sc.SubCategoryName,
                        HasChildren = false
                    })
                })
                .ToList();


            return Json(outputData);
        }
      
    }
    public class CategoryItem
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public List<SubCategoryItem> SubCategories { get; set; }
    }

    public class SubCategoryItem
    {
        public int SubCategoryId { get; set; }
        public string SubCategoryName { get; set; }
    }
}
