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;

namespace C1Extended_Demo
{
    public partial class PropertyGridNavigator : UserControl
    {
        private List<Product> _itemsSource;
        private int _currentItemIndex;

        public PropertyGridNavigator()
        {
            InitializeComponent();
            _itemsSource = ProductsData.GetProducts();
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void FirstItemButton_Click(object sender, RoutedEventArgs e)
        {
            _currentItemIndex = 0;
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void PreviousItemButton_Click(object sender, RoutedEventArgs e)
        {
            if (_currentItemIndex - 1 >= 0)
                _currentItemIndex--;
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void NextItemButton_Click(object sender, RoutedEventArgs e)
        {
            if (_currentItemIndex + 1 < _itemsSource.Count)
                _currentItemIndex++;
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void LastItemButton_Click(object sender, RoutedEventArgs e)
        {
            _currentItemIndex = _itemsSource.Count - 1;
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void NewItemButtonClick(object sender, RoutedEventArgs e)
        {
            _itemsSource.Add(new Product());
            _currentItemIndex = _itemsSource.Count - 1;
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }

        private void DeleteItemButtonClick(object sender, RoutedEventArgs e)
        {
            _itemsSource.RemoveAt(_currentItemIndex);
            if (_currentItemIndex >= _itemsSource.Count)
            {
                _currentItemIndex = _itemsSource.Count - 1;
            }
            _propertyGrid.SelectedObject = _itemsSource[_currentItemIndex];
        }
    }
}
