﻿using System;
using System.Windows;
using System.Windows.Input;

using DotNetBay.Core;
using DotNetBay.Model;
using DotNetBay.WPF.ViewModel.Common;

namespace DotNetBay.WPF.ViewModel
{
    public class BidViewModel : ViewModelBase
    {
        private readonly IAuctionService auctionService;

        private readonly Auction selectedAuction;

        public BidViewModel(Auction selectedAuction, IAuctionService auctionService)
        {
            this.auctionService = auctionService;
            this.selectedAuction = selectedAuction;

            this.CloseDialogCommand = new RelayCommand<Window>(this.CloseAction);
            this.AddBidAndCloseCommand = new RelayCommand<Window>(this.AddBidAndCloseAction);

            // Default Values
            this.YourBid = Math.Max(this.selectedAuction.CurrentPrice, this.selectedAuction.StartPrice);
        }

        public ICommand CloseDialogCommand { get; private set; }

        public ICommand AddBidAndCloseCommand { get; private set; }

        public double YourBid { get; set; }

        public string AuctionTitle
        {
            get { return this.selectedAuction.Title; }
        }

        public string AuctionDescription
        {
            get { return this.selectedAuction.Description; }
        }

        public byte[] AuctionImage
        {
            get { return this.selectedAuction.Image; }
        }

        public double AuctionStartPrice
        {
            get { return this.selectedAuction.StartPrice; }
        }

        public double AuctionCurrentPrice
        {
            get { return this.selectedAuction.CurrentPrice; }
        }

        public DateTime AuctionCloseDateTimeLocal
        {
            get { return this.selectedAuction.CloseDateTimeUtc.ToLocalTime(); }
        }

        private void CloseAction(Window window)
        {
            window.Close();
        }

        private void AddBidAndCloseAction(Window window)
        {
            this.auctionService.PlaceBid(this.selectedAuction, this.YourBid);

            window.Close();
        }
    }
}

