using FSI.BT.IR.Plc.Settings.Log.Data;
using FSI.BT.IR.Plc.Settings.Log.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace FSI.BT.IR.Plc.Settings.Log.Controllers
{
    public class PlcController : Controller
    {

        private readonly ApplicationDbContext _context;
        private readonly DataManager _dataManager;

        public PlcController(ApplicationDbContext context)
        {
            _context = context;
            _dataManager = new(context);
        }

        public async Task<IActionResult> IndexAsync()
        {
            Task<List<Models.Plc>> data = _dataManager.LoadPlcsAsync();

            return View(await data);
        }

        public async Task<IActionResult> AddOrEditAsync(int id = 0)
        {
            if (id == 0)
            {
                var item = new Models.Plc();
                item.Organizations = _context.Organizations.ToList();
             
                return View(item);
            }
            else
            {
                var item = await _context.Plcs.FindAsync(id);

                if (item == null)
                {
                    return NotFound();
                }

                item.Organizations = _context.Organizations.ToList();               
                return View(item);
            }
        }

        [HttpPost]
        public async Task<IActionResult> AddOrEditAsync(int id, Models.Plc item)
        {
            ModelState.Remove(nameof(item.Organization));

            item.Updated = DateTime.Now; // Zeitstempel für Modifikation 

            if (ModelState.IsValid)
            {
                if (id == 0) // Insert
                {
                    try
                    {
                        item.Created = item.Updated; // Zeitstempel für Erstellung
                        _context.Add(item);
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {

                    }
                }
                else // Update
                {
                    try
                    {
                        _context.Update(item);
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {

                    }
                }

                return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", await _dataManager.LoadPlcsAsync()) });
            }
            else
            {
                var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
            }
            
            return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", item) });
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmedAsync(int id)
        {
          
            var item = await _context.Plcs.FindAsync(id);
            _context.Plcs.Remove(item);

            await _context.SaveChangesAsync();

            return Json(new { html = Helper.RenderRazorViewToString(this, "_ViewAll", await _dataManager.LoadPlcsAsync()) });
        }


    }
}
