using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.ComponentModel;

namespace ItemTemplateSample
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            List<Person> persons = new List<Person>();

            persons.Add(new Person() { Name = "Jane Peterson" });
            persons.Add(new Person() { Name = "John Thompson" });
            persons.Add(new Person() { Name = "Kate Doe" });

            this.RadGridView.ItemsSource = persons;

        }

    }

    public class Person : INotifyPropertyChanged
    {
        public string Name { get; set; }
        public List<String> Hobbies { get; set; }

        public Person()
        {
            this.availableHobbies = new List<Hobby>();
            availableHobbies.Add(new Hobby() { Description = "1. bungee jumping", Owner=this });
            availableHobbies.Add(new Hobby() { Description = "2. hang gliding", Owner = this });
            availableHobbies.Add(new Hobby() { Description = "3. parachuting", Owner = this });
            availableHobbies.Add(new Hobby() { Description = "4. windsurfing", Owner = this });
            availableHobbies.Add(new Hobby() { Description = "5. hot air ballooning", Owner = this });
        }

        private List<Hobby> availableHobbies;
        public List<Hobby> AvailableHobbies
        {
            get
            {
                return availableHobbies;
            }
        }

        public string Choices
        {
            get
            {
                return string.Join(", ", (this.AvailableHobbies as IEnumerable<Hobby>).Where(h=>h.IsChecked==true).Select(item => item.Description).ToArray());
            }
        }

        public void OnCheckedStateChanged()
        {
            if(this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("Choices"));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Hobby
    {
        public string Description { get; set; }
        private bool isChecked;
        public bool IsChecked
        {
            get
            {
                return this.isChecked;
            }
            set
            {
                this.isChecked = value;
                this.Owner.OnCheckedStateChanged();
            }
        }

        public Person Owner { get; set; }
    }
}
