﻿using MusicStore.Api.Content;
using MusicStore.Api.Models;
using MusicStore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MusicStore.Api.Controllers
{
    public class ProducerController : BaseController
    {
        [HttpGet]
        public IHttpActionResult GetProducer()
        {
            return Ok(this.data.Producers.All()
                .Select(ProducerModel.FromProducer));
        }
        [HttpGet]
        public IHttpActionResult GetProducer(int id)
        {
            var producer = data.Producers.All()
                .FirstOrDefault(x => x.ProducerId == id);
            if (producer == null)
            {
                return BadRequest("No producer with such Id found.");
            }
            return Ok(ProducerModel.FromProducer.Compile()(producer));
        }
        [HttpPut]
        public IHttpActionResult CreateProducer(ProducerModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var producer = data.Producers.All()
                .FirstOrDefault(x => x.ProducerId == model.ProducerId);
            if (producer != null)
            {
                return BadRequest("There is such producer. Try to make the request without giving the producer id.");
            }
            producer = new Producer
            {
                ProducerName = model.ProducerName
            };
            data.Producers.Add(producer);
            data.SaveChanges();
            model.ProducerId = producer.ProducerId;
            return Ok(model);
        }
        [HttpDelete]
        public IHttpActionResult DeleteProducer(int id)
        {
            var producer = data.Producers.All()
                .FirstOrDefault(x => x.ProducerId == id);
            if (producer == null)
            {
                return BadRequest("No producer with such Id found.");
            }
            data.Producers.Delete(producer);
            data.SaveChanges();
            return Ok();
        }
        [HttpGet]
        public IHttpActionResult GetAlbums(int id)
        {
            var producer = data.Producers.All()
               .FirstOrDefault(x => x.ProducerId == id);
            if (producer == null)
            {
                return BadRequest("No producer with such Id found.");
            }
            return Ok(producer.Albums.Select(AlbumModel.FromModel.Compile()));
        }
        [HttpPost]
        public IHttpActionResult UpdateProducer(ProducerModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var producer = data.Producers.All()
                .FirstOrDefault(x => x.ProducerId == model.ProducerId);
            if (producer == null)
            {
                return BadRequest("There is such producer");
            }
            producer.ProducerName = model.ProducerName;
            data.SaveChanges();
            return Ok(model);
        }

    }
}
