﻿#region License

// Copyright 2014 MorseCode Software
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MorseCode.RxMvvm.UI.Wpf.Controls
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Reactive.Linq;
    using System.Windows.Controls;

    using MorseCode.RxMvvm.Common.DiscriminatedUnion;
    using MorseCode.RxMvvm.Observable;

    /// <summary>
    /// Provides label extension methods for binding.
    /// </summary>
    public static class LabelExtensionMethods
    {
        /// <summary>
        /// Binds the <see cref="Label.Content"/> property of a <see cref="Label"/>.
        /// </summary>
        /// <param name="label">
        /// The label.
        /// </param>
        /// <param name="dataContext">
        /// The data context.
        /// </param>
        /// <param name="getTextObservable">
        /// A delegate to get the text.
        /// </param>
        /// <param name="bindingFactory">
        /// The binding factory.
        /// </param>
        /// <typeparam name="T">
        /// The type of the data context.
        /// </typeparam>
        /// <returns>
        /// An <see cref="IBinding"/> which will clean up the bindings when disposed.
        /// </returns>
        public static IBinding BindContent<T>(
            this Label label,
            IObservable<T> dataContext,
            Func<T, IObservable<string>> getTextObservable,
            IBindingFactory<T> bindingFactory) where T : class
        {
            Contract.Requires<ArgumentNullException>(label != null, "label");
            Contract.Requires<ArgumentNullException>(dataContext != null, "dataContext");
            Contract.Requires<ArgumentNullException>(getTextObservable != null, "getTextProperty");
            Contract.Requires<ArgumentNullException>(bindingFactory != null, "bindingFactory");
            Contract.Ensures(Contract.Result<IBinding>() != null);

            return label.BindContent(
                dataContext,
                d => getTextObservable(d).Select(DiscriminatedUnion.First<object, string, NonComputable>),
                bindingFactory);
        }

        /// <summary>
        /// Binds the <see cref="Label.Content"/> property of a <see cref="Label"/>.
        /// </summary>
        /// <param name="label">
        /// The label.
        /// </param>
        /// <param name="dataContext">
        /// The data context.
        /// </param>
        /// <param name="getTextObservable">
        /// A delegate to get the text.
        /// </param>
        /// <param name="bindingFactory">
        /// The binding factory.
        /// </param>
        /// <typeparam name="T">
        /// The type of the data context.
        /// </typeparam>
        /// <returns>
        /// An <see cref="IBinding"/> which will clean up the bindings when disposed.
        /// </returns>
        public static IBinding BindContent<T>(
            this Label label,
            IObservable<T> dataContext,
            Func<T, IObservable<IDiscriminatedUnion<object, string, NonComputable>>> getTextObservable,
            IBindingFactory<T> bindingFactory) where T : class
        {
            Contract.Requires<ArgumentNullException>(label != null, "label");
            Contract.Requires<ArgumentNullException>(dataContext != null, "dataContext");
            Contract.Requires<ArgumentNullException>(getTextObservable != null, "getTextProperty");
            Contract.Requires<ArgumentNullException>(bindingFactory != null, "bindingFactory");
            Contract.Ensures(Contract.Result<IBinding>() != null);

            return bindingFactory.CreateChainedOneWayBinding(dataContext, getTextObservable, v => label.Content = v);
        }
    }
}