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 DelegateSample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //TODO: 1.声明MyDelegate变量
        MyDelegate handler;

        public MainWindow()
        {
            InitializeComponent();
        }

        //TODO: 2.根据MyDelegate规格建立问好的方法 
        private static void SayHello(ref string output, string param)
        {
            output += "Hello, " + param + ".\n";
        }

        private static void GoodMorning(ref string output, string param)
        {
            output += "早安, " + param + ".\n";
        }

        private static void GoodAfternoon(ref string output, string param)
        {
            output += "午安, " + param + ".\n";
        }

        private static void Goodbye(ref string output, string param)
        {
            output += "再见, " + param + ".\n";
        }

        private void btnExecute_Click(object sender, RoutedEventArgs e)
        {
            string data = "";
            if (handler != null)
            {
                //TODO: 6.通过handler委派对象触发方法执行
                handler.Invoke(ref data, txtName.Text);
            }

            tbResult.Text = data;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //取出被选取的CheckBox控件
            CheckBox chk = (CheckBox)sender;

            //TODO: 4.取出CheckBox控件对应的MyDelegate委派对象
            MyDelegate d = (MyDelegate)chk.Tag;

            //TODO: 5.根据CheckBox控件选取状态，将方法位置参考加入handler委派对象
            handler += d;
        }

        private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
        {
            //取出被选取的CheckBox控件
            CheckBox chk = (CheckBox)sender;

            //取出CheckBox控件对应的MyDelegate委派对象
            MyDelegate d = (MyDelegate)chk.Tag;

            //根据CheckBox控件选取状态，将方法位置参考从handler委派对象删除
            handler -= d;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //TODO: 3.将方法的位置参考纪录到对应的CheckBox控件中
            chkSayHello.Tag = new MyDelegate(SayHello);
            chkGoodMorning.Tag = new MyDelegate(GoodMorning);
            chkGoodAfternoon.Tag = new MyDelegate(GoodAfternoon);
            chkGoodBye.Tag = new MyDelegate(Goodbye);
        }


    }
}

