using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Runtime.Filters;
using ElectroShop.Models;
using ElectroShop.Services;
using ElectroShop.DAL.Entities;
using ElectroShop.Resources;

namespace ElectroShop.ViewModels.ElectronicComponent
{
    [Authorize(Roles = "Warehouseman")]
    public class DeleteViewModel : MasterPageViewModel
    {
        private readonly ElectronicComponentService electronicComponentService;

        public ElectronicComponentModel Model { get; set; } = new ElectronicComponentModel();

        public DeleteViewModel(ElectronicComponentService electronicComponentService)
        {
            this.electronicComponentService = electronicComponentService;
        }

        public override async Task PreRender()
        {
            await base.PreRender();
        }

        public override Task Init()
        {
            int id = 0;
            if (int.TryParse(Context.Parameters["Id"].ToString(), out id))
            {
                Model = electronicComponentService.GetById(id);
            }
            return base.Init();
        }

        public void Delete()
        {
            try
            {
                electronicComponentService.Delete(Model);
            }
            catch (Exception ex)
            {
                Context.RedirectToRoute("ElectronicComponentManagement");
            }
            finally
            {
                Context.RedirectToRoute("ElectronicComponentManagement");
            }

        }
    }
}
