using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using SPWA.DataAccess.Data.MJDbContext;
using SPWA.DataAccess.Data.Models;

namespace SPWA.DataAccess.Data.PlaceholderServices
{
    public class PlaceholderServices
    {


        private readonly MJContext _context;
        public PlaceholderServices(MJContext context)
        {
            _context = context;
        }


        public Task <List<Placeholder>> GetPlaceholderAsync(string strCurrentUser)
        {
            List<Placeholder> ColPlaceholder = new List<Placeholder>();
            ColPlaceholder = (from placeholder in _context.Placeholder select placeholder).ToList();
            return Task.FromResult(ColPlaceholder);
        }

        public Task<Placeholder> CreatePlaceholderAsync(Placeholder objPlaceholder)
        {
            _context.Placeholder.Add(objPlaceholder);
            _context.SaveChanges();
            return Task.FromResult(objPlaceholder);
        }


        public Task<bool> UpdatePlaceholderAsync(Placeholder objPlaceholder)
        {
            var ExistingPlaceholder =
                _context.Placeholder
                .Where(x => x.PlaceholderID == objPlaceholder.PlaceholderID)
                .FirstOrDefault();
            if (ExistingPlaceholder != null)
            {
                ExistingPlaceholder.Code =
                    objPlaceholder.Code;
                ExistingPlaceholder.text =
                    objPlaceholder.text;
                ExistingPlaceholder.Value =
                    objPlaceholder.Value;
                ExistingPlaceholder.TermID =
                    objPlaceholder.TermID;
                

                _context.SaveChanges();
            }
            else
            {
                return Task.FromResult(false);
            }
            return Task.FromResult(true);
        }



    }
}
