﻿using Newtonsoft.Json;
using RemoteConfig.Common.Interfaces;
using RemoteConfig.Common.MEX;
using RemoteConfig.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;

namespace RemoteConfig.Server.Web.Controllers
{
    [RoutePrefix("api/connectionStrings")]
    public class ApiConnectionStringsController : ApiController
    {
        IConnectionStringsService _cnStringService;
        public ApiConnectionStringsController(IConnectionStringsService cnStringService):base()
        {
            _cnStringService = cnStringService;
        }

        [Route(""), HttpGet]
        public IHttpActionResult GetSettings([FromUri] string[] app = null, [FromUri] string[] env = null, [FromUri] string[] revision = null)
        {
          
            var response = _cnStringService.GetSettings(app, env, revision, this.Url.Link,"details");

            return new NegotiatedContentResult<ConnectionStringsGetResponse>(response.Status.StatusCode, response, this);
        }

        [Route("{id}/details",Name="details"),HttpGet]
        public IHttpActionResult GetConnectionStringDetails(Guid id)
        {
            var response = _cnStringService.GetSettings(id);
            return new NegotiatedContentResult<ConnectionStringGetResponse>(response.Status.StatusCode, response, this);
           
        }

        [Route("{id}/edit", Name = "settingEdit"), HttpGet]
        public IHttpActionResult GetSettingForEdit(Guid id)
        {
            var response = _cnStringService.GetSettingForEdit(id);
            return new NegotiatedContentResult<ConnectionStringGetEditorResponse>(response.Status.StatusCode, response, this);
        }

        [Route("",Name="createConnectionString"),HttpPost]
        public IHttpActionResult CreateConnectionString([FromBody]ConnectionStringCreateRequest value)
        {
            _cnStringService.CreateSetting(value);

            var httpResponse = new NegotiatedContentResult<ConnectionStringCreateResponse>(HttpStatusCode.OK, null, this);

            return httpResponse;
            
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}