﻿using System;
using Windows.UI.Xaml;

namespace TheChan.Views.Controls {
    public sealed partial class LoadingControl {

        public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(
            "Status", typeof (string), typeof (LoadingControl), new PropertyMetadata(default(string)));

        public string Status {
            get { return (string) GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register(
            "IsError", typeof (bool), typeof (LoadingControl), new PropertyMetadata(default(bool), ProgressPropertyChangedCallback));

        public bool IsError {
            get { return (bool) GetValue(IsErrorProperty); }
            set { SetValue(IsErrorProperty, value); }
        }

        public static readonly DependencyProperty IsInProgressProperty = DependencyProperty.Register(
            "IsInProgress", typeof (bool), typeof (LoadingControl), new PropertyMetadata(default(bool), ProgressPropertyChangedCallback));


        public bool IsInProgress {
            get { return (bool) GetValue(IsInProgressProperty); }
            set { SetValue(IsInProgressProperty, value); }
        }

        public static readonly DependencyProperty CanTryAgainProperty = DependencyProperty.Register(
            "CanTryAgain", typeof (bool), typeof (LoadingControl), new PropertyMetadata(default(bool), ProgressPropertyChangedCallback));

        public bool CanTryAgain {
            get { return (bool) GetValue(CanTryAgainProperty); }
            set { SetValue(CanTryAgainProperty, value); }
        }

        private static readonly DependencyProperty IsStatusVisibleProperty = DependencyProperty.Register(
            "IsStatusVisible", typeof (bool), typeof (LoadingControl), new PropertyMetadata(default(bool)));

        private bool IsStatusVisible {
            get { return (bool) GetValue(IsStatusVisibleProperty); }
            set { SetValue(IsStatusVisibleProperty, value); }
        }

        public static readonly DependencyProperty TryAgainActionProperty = DependencyProperty.Register(
            "TryAgainAction", typeof (Action), typeof (LoadingControl), new PropertyMetadata(default(Action)));

        public Action TryAgainAction {
            get { return (Action) GetValue(TryAgainActionProperty); }
            set { SetValue(TryAgainActionProperty, value); }
        }

        private static readonly DependencyProperty IsTryAgainButtonVisibleProperty = DependencyProperty.Register(
            "IsTryAgainButtonVisible", typeof (bool), typeof (LoadingControl), new PropertyMetadata(default(bool)));

        private bool IsTryAgainButtonVisible {
            get { return (bool) GetValue(IsTryAgainButtonVisibleProperty); }
            set { SetValue(IsTryAgainButtonVisibleProperty, value); }
        }

        private static void ProgressPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
            var isLoading = (bool)dependencyObject.GetValue(IsInProgressProperty);
            var isError = (bool)dependencyObject.GetValue(IsErrorProperty);
            dependencyObject.SetValue(IsStatusVisibleProperty, isError || isLoading);

            var canTryAgain = (bool) dependencyObject.GetValue(CanTryAgainProperty);
            dependencyObject.SetValue(IsTryAgainButtonVisibleProperty, isError && canTryAgain);
        }

        public LoadingControl() {
            InitializeComponent();
        }

        private void TryAgainButton_OnClick(object sender, RoutedEventArgs e) {
            TryAgainAction?.Invoke();
        }
    }
}
