using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using AvaloniaApplication1.Controls.Common;

namespace AvaloniaApplication1.Controls
{
    [TemplatePart(CS_NAME_BORDER, typeof(Border))]
    public class StickyLabel : TemplatedControl
    {
        #region *** Private Filelds ***
        private Border? _border = null;
        #endregion

        #region *** Properties ***
        private const string CS_NAME_BORDER = "PART_StickyLabel";

        public static readonly StyledProperty<string> TextProperty =
            AvaloniaProperty.Register<StickyLabel, string>(
                nameof(StickyLabel),
                defaultValue: string.Empty);
        public string Text
        {
            get => GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }


        public static readonly StyledProperty<SideOrientationEnum> SideOrientationProperty =
            AvaloniaProperty.Register<StickyLabel, SideOrientationEnum>(
                nameof(StickyLabel),
                defaultValue: SideOrientationEnum.Right);
        public SideOrientationEnum SideOrientation
        {
            get => GetValue(SideOrientationProperty);
            set => SetOrientation(value, CornerSize);
        }

        public static readonly StyledProperty<double> CornerSizeProperty =
            AvaloniaProperty.Register<StickyLabel, double>(
                nameof(StickyLabel),
                defaultValue: 10);
        public double CornerSize
        {
            get => GetValue(CornerSizeProperty);
            set => SetOrientation(SideOrientation, value);
        }

        public static readonly StyledProperty<BoxShadows> BoxShadowProperty =
            AvaloniaProperty.Register<StickyLabel, BoxShadows>(
                nameof(StickyLabel),
                defaultValue: BoxShadows.Parse("0 0 0 0 DarkGray"));
        public BoxShadows BoxShadow
        {
            get => GetValue(BoxShadowProperty);
            set => SetBoxShadow(value);
        }
        #endregion

        #region *** Constructors ***
        public StickyLabel()
        {
        }
        #endregion

        #region *** Overriden Methods ***

        protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
        {
            _border = e.NameScope.Find<Border>(CS_NAME_BORDER);
            SetOrientation(SideOrientation, CornerSize);
        }
        #endregion

        #region *** Private Methods ***
        private void SetBoxShadow(BoxShadows boxShadow)
        { 
            SetValue(BoxShadowProperty, boxShadow);
            if (_border == null) return;
            _border.BoxShadow = boxShadow;
        }


        private void SetOrientation(SideOrientationEnum? sideOrientation, double? cornerSize)
        {

            if (sideOrientation != null)
                SetValue(SideOrientationProperty, sideOrientation);
            else
                sideOrientation = SideOrientation;
            if (cornerSize != null)
                SetValue(CornerSizeProperty, cornerSize);
            else
                cornerSize = CornerSize;
            if (cornerSize == null) throw new ArgumentNullException(nameof(cornerSize));

            if (_border == null) return;

            switch (sideOrientation)
            {
                case SideOrientationEnum.Left:
                    _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), 0, 0, cornerSize.GetValueOrDefault());
                    break;
                case SideOrientationEnum.Right:
                    _border.CornerRadius = new CornerRadius(0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0);
                    break;
                case SideOrientationEnum.Top:
                    _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0, 0);
                    break;
                case SideOrientationEnum.Down:
                    _border.CornerRadius = new CornerRadius(0, 0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault());
                    break;
            }
        }
        #endregion
    }
}
