using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DebugTool
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        enum SalaryLevelMode
        {
            Low,
            Default,
            High
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            decimal salary, actualSalary;
            string name;
            DateTime hireDate;
            decimal taxRate = 0.1M;
            int hireYear, bonus;
            SalaryLevelMode salaryLevel;

            //TODO: 1.驗證是否輸入資料
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("請輸入[姓名]");
                return;  //結束副程式
            }
            if (!DateTime.TryParse(txtHireDate.Text, out hireDate))
            {
                MessageBox.Show("[到職日]請輸入日期格式");
                return;  //結束副程式
            }
            if (!decimal.TryParse(txtSalary.Text, out salary))
            {
                MessageBox.Show("[月薪]請輸入數字格式");
                return;  //結束副程式
            }


            salary = decimal.Parse(txtSalary.Text);
            name = txtName.Text;

            //TODO: 3.使用try檢驗以下程式是否正常

                hireDate = DateTime.Parse(txtHireDate.Text);
                hireYear = hireDate.Year;
                bonus = GetBonus(hireYear, salary);
                salaryLevel = setTaxRate(salary, ref taxRate);

                //實領薪 = 月薪 - 稅額 (月薪 * 稅率)
                actualSalary = salary - (salary * taxRate) + bonus;
                tbResult.Text = name + "於" + hireDate +
                        "到職, 本月實領薪資為" + actualSalary
                        + "(含紅利" + bonus + ")，稅率為" + (taxRate * 100) + "%.";


        }

        private SalaryLevelMode setTaxRate(decimal salary, ref decimal taxRate)
        {
            SalaryLevelMode salaryLevel;
            if (salary < 3000)
            {
                salaryLevel = SalaryLevelMode.Low;
            }
            else if (salary < 100000)
            {
                salaryLevel = SalaryLevelMode.Default;
            }
            else
            {
                salaryLevel = SalaryLevelMode.High;
            }

            switch (salaryLevel)
            {
                case SalaryLevelMode.Low:
                    taxRate = 0.03M;
                    break;
                case SalaryLevelMode.Default:
                    taxRate = 0.05M;
                    break;
                default:
                    taxRate = 0.1M;
                    break;
            }
            return salaryLevel;
        }

        private int GetBonus(int hireYear, decimal salary)
        {
            //TODO: 2.判斷[薪水]是否小等於0，如果是，產生例外狀況ArgumentException
            if (salary <= 0)
            {
                    throw new ArgumentException("[薪水]小等於0, 無法計算紅利金額.");
            }

            int bonus;
            if ((hireYear <= 2000) || (salary > 50000))
            {
                bonus = 3000;
            }
            else if ((hireYear <= 2003) && (salary > 30000))
            {
                bonus = 1500;
            }
            else
            {
                bonus = 500;
            }
            return bonus;
        }
    }
}
