﻿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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WitControl
{
    /// <summary>
    /// Interaction logic for SimpleTabControl.xaml
    /// </summary>
    public partial class SimpleTabControl : TabControl
    {



        ContentPresenter CurrentItem { get; set; }
        ContentPresenter BeforeItem { get; set; }

        DoubleAnimation FadeIn = new DoubleAnimation(0, 1, Duration.Automatic, FillBehavior.HoldEnd) { SpeedRatio = 2, AccelerationRatio = 0.7 };
        DoubleAnimation FadeOut = new DoubleAnimation(1, 0, Duration.Automatic, FillBehavior.HoldEnd) { SpeedRatio = 2, AccelerationRatio = 0.7 };
           

        public SimpleTabControl()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(SimpleTabControl_Loaded);
            SelectionChanged += new SelectionChangedEventHandler(SimpleTabControl_SelectionChanged);

            FadeOut.Completed += new EventHandler(FadeOut_Completed);
            
        }

        void FadeOut_Completed(object sender, EventArgs e)
        {
            BeforeItem.Visibility = Visibility.Collapsed;
        }

        void SimpleTabControl_Loaded(object sender, RoutedEventArgs e)
        {
        
            CurrentItem = GetTemplateChild("CurrentItem") as ContentPresenter;
            BeforeItem = GetTemplateChild("BeforeItem") as ContentPresenter;
            CurrentItem.Content = SelectedContent;

        }

        

        void SimpleTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (CurrentItem == null) return;

            CurrentItem.Content = (e.AddedItems[0] as ContentControl).Content;
            BeforeItem.Content = (e.RemovedItems[0] as ContentControl).Content;


            BeforeItem.Visibility = Visibility.Visible;
            CurrentItem.BeginAnimation(ContentPresenter.OpacityProperty, FadeIn);
            BeforeItem.BeginAnimation(ContentPresenter.OpacityProperty, FadeOut);
            
        }
    }
}

