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

namespace DDT_Demo_Core3._1.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();
        }

        public static IList<HierarchicalViewModel> GetHierarchicalData()
        {
            var result = new List<HierarchicalViewModel>()
            {
                new HierarchicalViewModel() { ID = "1", ParentID = null, HasChildren = true, Name = "Parent item" },
                new HierarchicalViewModel() { ID = "2", ParentID = 1, HasChildren = true, Name = "Parent item" },
                new HierarchicalViewModel() { ID = "3", ParentID = 1, HasChildren = false, Name = "Item 1" },
                new HierarchicalViewModel() { ID = "4", ParentID = 2, HasChildren = false, Name = "Item 2" },
                new HierarchicalViewModel() { ID = "5", ParentID = 2, HasChildren = false, Name = "Item 3" }
            };

            return result;
        }

        public IActionResult Read_DropDownTreeData(string id, string param, int param2)
        {
            var result = GetHierarchicalData()
                .Where(x => String.IsNullOrEmpty(id) ? x.ParentID == null : x.ParentID.ToString() == id)
                .Select(item => new {
                    id = item.ID,
                    Name = item.Name,
                    hasChildren = item.HasChildren
                });

            return Json(result);
        }
    }
}
