using Demo_Project.Model;
using Demo_Project.Service;
using Demo_Project.Shape;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.Windows.Diagrams.Core;

namespace Demo_Project
{
    public partial class DistributionHall : Form
    {
        private DistributionHallManager distributionHallManager;
        private ShelveModel selectedShelve;
        public DistributionHall()
        {
            InitializeComponent();

            radDiagram.ServiceLocator.Register<IRotationService>(new GroupRotationService(radDiagram.DiagramElement));

            distributionHallManager = new DistributionHallManager();

            radDiagram.AllowCopy = false;
            radDiagram.AllowPaste = false;
            radDiagram.SelectionMode = Telerik.Windows.Diagrams.Core.SelectionMode.Single;
            radDiagram.ShapeClicked += RadDiagram_ShapeClicked;

            distributionHallManager.LoadHallData(radDiagram);
            distributionHallManager.DrawShelves();

            buttonSwitchOrientation.Click += ButtonSwitchOrientation_Click;
            buttonRefresh.Click += ButtonRefresh_Click;
        }

        private void RadDiagram_ShapeClicked(object sender, Telerik.WinControls.UI.Diagrams.ShapeRoutedEventArgs e)
        {
            var clickedShapeModel = ((RadDiagramShape)e.Shape).Tag as BaseModel;
            if (clickedShapeModel is SpotModel spot)
            {
                selectedShelve = spot.Shelve;
            }
            else if (clickedShapeModel is ShelveModel shelve)
            {
                selectedShelve = shelve;
            }
        }

        private void ButtonRefresh_Click(object sender, EventArgs e)
        {
            radDiagram.SuspendLayout();
            radDiagram.Items.Clear();
            distributionHallManager.DrawShelves();
            radDiagram.ResumeLayout();
        }

        private void ButtonSwitchOrientation_Click(object sender, EventArgs e)
        {
            var currentOrientation = selectedShelve?.Orientation;
            switch (currentOrientation)
            {
                case ShelveOrientation.LeftToRight:
                    selectedShelve.SetRotation(ShelveOrientation.BottomToTop);
                    break;
                case ShelveOrientation.BottomToTop:
                    selectedShelve.SetRotation(ShelveOrientation.LeftToRight);
                    break;
                default:
                    break;
            }
        }
    }
}
