using System;
using System.Windows.Forms;

namespace Cheongbuk_MegaPark_Key_Management
{
    public partial class ReturnKey : Form
    {
        private int _price;
        private int _priceDiscount;
        private int _priceFinal;
        private ProfitData _profitData;
        public RentData Data;
        public ProfitData ReturnData;

        public ReturnKey()
        {
            InitializeComponent();
            _price = 0;
            _priceDiscount = 0;
            _priceFinal = 0;
        }

        private void Button_ReturnOk_Click(object sender, EventArgs e)
        {
            _profitData.ProfitAdd = _price;
            _profitData.ProfitDiscount = _priceDiscount;
            ReturnData = _profitData;
        }

        private void ReturnKey_Shown(object sender, EventArgs e)
        {
            _profitData = new ProfitData();
            _profitData.KeyCode = Data.KeyCode;
            _profitData.Category = Data.Category;
            _profitData.StartDateTime = Data.StartDateTime;
            _profitData.People = Data.People;
            _profitData.EndDateTime = DateTime.Now;
            _profitData.ProfitBase = Setting.CategoryDictionary[Data.Category].CategoryProfit * Data.People;
            var elapsedTime = (int)(DateTime.Now - Data.StartDateTime).TotalMinutes;
            var elapsedTimeH = elapsedTime / 60;
            var elapsedTimeM = elapsedTime % 60;
            label_ElapsedTime.Text = elapsedTimeH.ToThousand() + "시간 " + elapsedTimeM.ToThousand() + "분";
            
                var useTime = elapsedTime - (_profitData.Category * 60);
                _profitData.UseTime = useTime > 0 ? useTime : 0;
                _price = _profitData.UseTime / Setting.OverTime * Setting.OverAmount * Data.People;

                // 할인 버튼 활성화
            

            label_KeyCode.Text = _profitData.KeyCode.ToString();
            label_Category.Text = Setting.CategoryDictionary[_profitData.Category].CategoryName;
            label_People.Text = _profitData.People.ToPeople();
            label_StartDateTime.Text = _profitData.StartDateTime.ToString("yyyy/MM/dd HH:mm:ss");

            var useTimeH = _profitData.UseTime / 60;
            var useTimeM = _profitData.UseTime % 60;
            label_UseTime.Text = useTimeH.ToThousand() + "시간 " + useTimeM.ToThousand() + "분";

            ChangePrice();
            button_ReturnOk.Focus();
        }

        private void ChangePrice()
        {
            _priceFinal = _price - _priceDiscount;
            label_Price.Text = _price.ToMoney();
            label_PriceDiscount.Text = _priceDiscount.ToMoney();
            label_PriceFinal.Text = _priceFinal.ToMoney();
        }

        private void Button_DC_Click(object sender, EventArgs e)
        {
            int.TryParse(((Button)sender).Tag.ToString(), out var value);
            if (value == -1)
            {
                _priceDiscount = 0;
                _priceFinal = _price;
            }
            else
            {
                int sumDiscount = 0;
                if (value == 91)
                {
                    sumDiscount = _priceDiscount + (Setting.DiscountDatas[value].Value * _profitData.People);
                }
                else if(value ==0)
                {
                    sumDiscount = _price;
                }
                else
                {
                    sumDiscount = _priceDiscount + Setting.DiscountDatas[value].Value;
                }

                if (sumDiscount <= _price)
                {
                    _priceDiscount = sumDiscount;
                    _priceFinal = _price - _priceDiscount;
                }
                else
                {
                    _priceDiscount = _price;
                    _priceFinal = 0;
                }
            }

            ChangePrice();
        }
    }
}