using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;

namespace rockfishCore.Controllers
{
    [Produces("application/json")]
    [Route("api/TextTemplate")]
    [Authorize]
    public class TextTemplateController : Controller
    {
        private readonly rockfishContext _context;

        public TextTemplateController(rockfishContext context)
        {
            _context = context;
        }


        //ui ordered list
        [HttpGet("list")]
        public IEnumerable<TextTemplate> GetList()
        {
            var tt = from s in _context.TextTemplate select s;
            tt = tt.OrderBy(s => s.Name);
            return tt;
        }

        // GET: api/TextTemplate
        [HttpGet]
        public IEnumerable<TextTemplate> GetTextTemplate()
        {
            return _context.TextTemplate;
        }

        // GET: api/TextTemplate/5
        [HttpGet("{id}")]
        public async Task<IActionResult> GetTextTemplate([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var TextTemplate = await _context.TextTemplate.SingleOrDefaultAsync(m => m.Id == id);

            if (TextTemplate == null)
            {
                return NotFound();
            }

            return Ok(TextTemplate);
        }

        // PUT: api/TextTemplate/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutTextTemplate([FromRoute] long id, [FromBody] TextTemplate TextTemplate)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != TextTemplate.Id)
            {
                return BadRequest();
            }

            _context.Entry(TextTemplate).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TextTemplateExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/TextTemplate
        [HttpPost]
        public async Task<IActionResult> PostTextTemplate([FromBody] TextTemplate TextTemplate)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.TextTemplate.Add(TextTemplate);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetTextTemplate", new { id = TextTemplate.Id }, TextTemplate);
        }

        // DELETE: api/TextTemplate/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteTextTemplate([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var TextTemplate = await _context.TextTemplate.SingleOrDefaultAsync(m => m.Id == id);
            if (TextTemplate == null)
            {
                return NotFound();
            }

            _context.TextTemplate.Remove(TextTemplate);
            await _context.SaveChangesAsync();

            return Ok(TextTemplate);
        }

        private bool TextTemplateExists(long id)
        {
            return _context.TextTemplate.Any(e => e.Id == id);
        }
    }
}