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 HRApplication
{
    /// <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;
            int hireYear, bonus;

            //TODO:1.修改初始化的程式
            decimal taxRate = 0.1M;

            #region 验证是否输入资料
            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;  //结束子程序
            }
            #endregion

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

                hireDate = DateTime.Parse(txtHireDate.Text);
                hireYear = hireDate.Year;

                bonus = GetBonus(salary, hireYear);

                //TODO:3.修改调用子程序的代码
                SetTaxRate(salary, ref taxRate);

                //实际领取薪资= 月薪 - 税额 (月薪 * 稅率)
                actualSalary = salary - (salary * taxRate) + bonus;
                tbResult.Text = name + "於" + hireDate +
                        "入职, 本月实际领取薪资为" + actualSalary
                        + "(含红利" + bonus + ")，稅率为" + (taxRate * 100) + "%.";


        }


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

            return bonus;
        }

        //TODO:2.将taxRate参数声明成输出参数
        private void 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;
            }
        }

    }
}
