using Domain.BaseData;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Models.BaseData;
using Services.BaseData;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Api.Controllers
{
    [Authorize(Roles = Consts.Developer)]

    public class PlantsController : BaseController
    {
        private readonly IPlantsService plantsService;
        public PlantsController(IServiceProvider serviceProvider) : base(serviceProvider)
        {
            plantsService = serviceProvider.GetService<IPlantsService>();
        }

        [HttpGet]
        public IActionResult List()
        {
            var dbList = plantsService.GetQueryable().ToList();
            return Ok(Mapper.Map<List<PlantViewModel>>(dbList));
        }
        [HttpGet("{id}")]
        public IActionResult Get(Guid id)
        {
            var pl = plantsService.GetById(id);
            return Ok(Mapper.Map<SavePlantViewModel>(pl));
        }
        [HttpPost]
        public IActionResult Add(SavePlantViewModel model)
        {
            var dbModel = Mapper.Map<Plant>(model);
            var imageName=SaveImageFromBase64(model.Base64Icon);
            dbModel.ImageFileName = imageName;
            plantsService.Add(dbModel);
            return Ok();
        }
        [HttpPut]
        public IActionResult Update(PlantViewModel model)
        {
            var dbModel = plantsService.GetById(model.Id.Value);
            Mapper.Map(model, dbModel);
            plantsService.Update(dbModel);
            return Ok();
        }
        [HttpDelete("{id}")]
        public IActionResult Delete(Guid id)
        {
            plantsService.Delete(id);
            return Ok();
        }

    }
}
