using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Nevyn.Models;
using Nevyn.Classes;

namespace Nevyn.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MoneyController : ControllerBase
    {
        private readonly MoneyContext _context;
        public MoneyController(MoneyContext context)
        {
            _context = context;
        }

        // GET: api/ShoppingLists
        [HttpGet]
        public async Task<ActionResult<WalletDTO>> GetWallet()
        {
            Wallet wallet = await _context.UpdateWallet.FindAsync(1);

            if (wallet == null)
            {
                wallet = new Wallet();
                wallet.updateFromDatabase();
                _context.UpdateWallet.Add(wallet);
                await _context.SaveChangesAsync();

            }
            return ItemToDTO(wallet);
        }

        // GET: api/ShoppingLists/5
        [HttpGet("{id}")]
        public async Task<ActionResult<WalletDTO>> GetWallet(long id)
        {
            var wallet = await _context.UpdateWallet.FindAsync(id);

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

            return ItemToDTO(wallet);
        }

        // PUT: api/ShoppingLists/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut]
        public async Task<IActionResult> PutWallet(long id, Wallet wallet)
        {

            var currentWallet = await _context.UpdateWallet.FindAsync(wallet.Id);
            if (currentWallet == null)
            {
                return NotFound();
            }
            currentWallet.Kort = wallet.Kort;
            currentWallet.Buffert = wallet.Buffert;
            currentWallet.Fonder = wallet.Fonder;
            currentWallet.Spara = wallet.Spara;


            await _context.SaveChangesAsync();

            return NoContent();
        }

        // POST: api/ShoppingLists
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<WalletDTO>> PostWallet(Wallet inputWallet)
        {

            var wallet = new Wallet
            {
                Buffert = inputWallet.Buffert,
                Fonder = inputWallet.Fonder,
                Kort = inputWallet.Kort,
                Spara = inputWallet.Spara,
            };

            _context.UpdateWallet.Add(wallet);
            await _context.SaveChangesAsync();

            return CreatedAtAction(
                nameof(GetWallet),
                new { id = wallet.Id },
                ItemToDTO(wallet));
        }

        private static WalletDTO ItemToDTO(Wallet wallet) =>
        new WalletDTO
        {
            Kort = wallet.Kort,
            TotalAmount = wallet.TotalAmount,
            AmountLeftPerDay = wallet.MoneyUntilSalary,
            Debug = "There is " + wallet.DaysLeftUntilSalary + " days left",
        };
    }
}
