﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using HomeBudget.Dao.Base.Interfaces;
using HomeManager.Model.Bo.Impl;

namespace HomeBudget.Forms.EntityForms
{
    public partial class MonthlyPaymentEntityForm : XtraForm, IEntityForm
    {
        private readonly IMonthlyPaymentRepository _monthlyPaymentRepository;
        private MonthlyPaymentBo _currentMonthlyPayment;

        public MonthlyPaymentEntityForm(IMonthlyPaymentRepository monthlyPaymentRepository)
        {
            _monthlyPaymentRepository = monthlyPaymentRepository;
            InitializeComponent();
            this.Load += MonthlyPaymentForm_Load;
        }

        public void SetCurrentId(int currentId)
        {
            _currentMonthlyPayment = _monthlyPaymentRepository.Get(currentId);
        }

        void MonthlyPaymentForm_Load(object sender, EventArgs e)
        {
            if (_currentMonthlyPayment != null)
            {
                txtDescription.Text = _currentMonthlyPayment.Description;
                clcEstimatedAmount.EditValue = _currentMonthlyPayment.EstimatedAmount;
                dateEditStartDate.EditValue = _currentMonthlyPayment.StartDate;
                dateEditEndDate.EditValue = _currentMonthlyPayment.EndDate;
                clcBillingStartDay.EditValue = _currentMonthlyPayment.BillingStartDay;
                clcBillingEndDay.EditValue = _currentMonthlyPayment.BillingEndDay;
                chkCompleted.Checked = _currentMonthlyPayment.Completed.GetValueOrDefault();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (IsDataValid())
            {
                SavePayment();
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void SavePayment()
        {
            if (_currentMonthlyPayment == null)
            {
                _currentMonthlyPayment = new MonthlyPaymentBo();
            }
            _currentMonthlyPayment.Description = txtDescription.Text;
            
            if (clcEstimatedAmount.EditValue != null)
                _currentMonthlyPayment.EstimatedAmount = clcEstimatedAmount.Value;
            else
                _currentMonthlyPayment.EstimatedAmount = null;

            if (clcBillingStartDay.EditValue != null)
                _currentMonthlyPayment.BillingStartDay = (byte)clcBillingStartDay.Value;
            else
                _currentMonthlyPayment.BillingStartDay = null;

            if (clcBillingEndDay.EditValue != null)
                _currentMonthlyPayment.BillingEndDay = (byte)clcBillingEndDay.Value;
            else
                _currentMonthlyPayment.BillingEndDay = null;

            if (dateEditStartDate.EditValue != null)
                _currentMonthlyPayment.StartDate = dateEditStartDate.DateTime;
            else
                _currentMonthlyPayment.StartDate = null;

            if (dateEditEndDate.EditValue != null)
                _currentMonthlyPayment.EndDate = dateEditEndDate.DateTime;
            else
                _currentMonthlyPayment.EndDate = null;

            _currentMonthlyPayment.Completed = chkCompleted.Checked;

            _monthlyPaymentRepository.Save(_currentMonthlyPayment);
        }

        private bool IsDataValid()
        {
            if (string.IsNullOrWhiteSpace(txtDescription.Text))
            {
                MessageBox.Show("Please enter a description!");
                return false;
            }
            return true;
        }
    }
}
