using Demo_Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.WinControls.UI;
using Telerik.Windows.Diagrams.Core;

namespace Demo_Project.Service
{
    public class GroupRotationService : RotationService
    {
        private double? customAngle = null;

        public GroupRotationService(IGraphInternal graph) : base(graph) { }

        public void RotateGroup(IGroup group, ShelveOrientation newOrientation)
        {
            var groupItems = group.Items.OfType<IDiagramItem>();
            var shelve = groupItems.Where(x => x is RadDiagramShape shape && shape.Tag is ShelveModel).Select(x => ((RadDiagramShape)x).Tag).FirstOrDefault() as ShelveModel;
            this.InitializeRotate(groupItems, 0, group.GetBounds());
            this.StartRotate(new Point());

            customAngle = getRotationAngle(newOrientation, shelve.Orientation);

            this.Rotate(new Point());
            this.customAngle = null;

            this.CompleteRotate(new Point());
        }

        protected override double CalculateRotationAngle(Point newPoint)
        {
            if (this.customAngle.HasValue)
                return this.customAngle.Value;
            else
                return base.CalculateRotationAngle(newPoint);
        }
        private double getRotationAngle(ShelveOrientation newOrientation, ShelveOrientation oldOrientation)
        {
            if (newOrientation == oldOrientation)
            {
                return newOrientation == ShelveOrientation.LeftToRight ? 0d : 270d;
            }
            else
            {
                return newOrientation == ShelveOrientation.LeftToRight ? 90d : 270d;
            }
        }
    }
}
