using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Nodes;

namespace WebApplication2.Controllers.WebNew
{
    public class BeritaController : Controller
    {
		[HttpGet("/WebNew/Berita")]
		public IActionResult Index()
        {
            return View("~/Views/WebNew/Berita/Berita.cshtml");
        }

        [HttpGet("/WebNew/Berita/GetBerita")]
        public IActionResult GetBerita()
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/webnew/json", "berita.json");

            if (!System.IO.File.Exists(path))
                return NotFound();

            var json = System.IO.File.ReadAllText(path);
            return Content(json, "application/json");
        }

		[HttpGet("/WebNew/Berita/Detail/")]
        public IActionResult Detail(int id)
        {
            ViewBag.Id = id;
            return View("~/Views/WebNew/Berita/DetailBerita.cshtml");
        }

		[HttpGet("/WebNew/Berita/GetDetail/")]
        public IActionResult GetDetail(int id)
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/webnew/json", "berita.json");

            if (!System.IO.File.Exists(path))
                return NotFound();

            var json = System.IO.File.ReadAllText(path);

            var data = JsonNode.Parse(json).AsArray();

            var result = data.FirstOrDefault(x => (int)x["id"] == id);

            if (result == null)
                return NotFound();

            return Json(result);
        }

		[HttpGet("/WebNew/Berita/GetAll")]
        public IActionResult GetAll()
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/webnew/json", "berita.json");
            var json = System.IO.File.ReadAllText(path);
            return Content(json, "application/json");
        }
    }
}