using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using UnivateProperties_API.Model.Properties;
using UnivateProperties_API.Repository.Properties;

namespace UnivateProperties_API.Controllers.Properties
{
    [Route("api/[controller]")]
    [ApiController]
    public class PropertyController : ControllerBase
    {
        private readonly IPropertyRepository _Repo;

        public PropertyController(IPropertyRepository repo)
        {
            _Repo = repo;
        }

        #region Get Methods
        [HttpGet]
        public IActionResult Get()
        {
            return new OkObjectResult(_Repo.GetDisplay());
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
        }

        [HttpGet("search/{keyword}")]
        public IActionResult Search(string Keyword)
        {
            return new OkObjectResult(_Repo.GetDisplay(Keyword));
        }

        [HttpGet("search/{type}/{propertytype}/{province}/{city}/{suburb}/{proptype}")]
        public IActionResult Get(string type, string propertyType, string province, string city, string suburb, string propType)
        {
            return new OkObjectResult(_Repo.GetDisplay(type, propertyType, province, city, suburb, propType));
        }

        [HttpGet("latestProperties")]
        public IActionResult GetLatestProperties()
        {
            return new OkObjectResult(_Repo.GetLatestDisplay());
        }

        [HttpGet("{type}/{by}")]
        public IActionResult SearchBy(string type, string by)
        {
            PropertyUsageType pType = PropertyUsageType.Both;
            switch (type.ToUpper())
            {
                case "RESIDENTIAL":
                    pType = PropertyUsageType.Residential;
                    break;
                case "COMMERCIAL":
                    pType = PropertyUsageType.Commercial;
                    break;
            }

            if (pType != PropertyUsageType.Both)
            {
                List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
                                         select pt.Id).ToList();

                if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
                {
                    return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
                }
                else
                {
                    //Needs to change to search on individule/Agent
                    return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
                }
            }
            else
                return new NoContentResult();
        }

        [HttpGet("GetEditDisplay/{id}")]
        public IActionResult GetEditDisplay(int id)
        {
            return new OkObjectResult(_Repo.GetPropertyList(x => x.Id == id));
        }
        #endregion

        [HttpPost]
        public IActionResult Post([FromBody] Property property)
        {
            using (var scope = new TransactionScope())
            {
                _Repo.Insert(property);
                scope.Complete();
                return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
            }
        }

        [HttpPut]
        public IActionResult Put([FromBody] Property property)
        {
            if (property != null)
            {
                using (var scope = new TransactionScope())
                {
                    _Repo.Update(property);
                    scope.Complete();
                    return new OkResult();
                }
            }
            return new NoContentResult();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            _Repo.RemoveAtId(id);
            return new OkResult();
        }
    }
}