@rendermode InteractiveServer
@if (GroupSelected)
{
	<MudPaper @onclick="HandleClick" Class="pa-1 ma-1" Outlined="false" Elevation="0">

		<MudStack Row="true">

			<MudButton Color="Color.Surface" Variant="Variant.Outlined" OnClick="OnAddText"> TEXT </MudButton>
			<MudButton Color="Color.Surface" Variant="Variant.Outlined" OnClick="OnAddRadio"> Radio </MudButton>
			
@* 			<MudSelect @bind-Value="GropType" Variant="Variant.Outlined">
				<MudSelectItem Value="@(GropType.Stack)" />
				<MudSelectItem Value="@(GropType.Grid)" />
			</MudSelect> *@
		</MudStack>

		<MudDivider/>
		@switch (@GropType)
		{
			case GropType.Stack:
				<MudStack Spacing="6">

					@foreach (var question in QuestionGroupElement.GroupsQuestions)
					{
						<QuestionBase QuestionElement="question"
									  IsSelected="question.IsSelected"
									  MoveDown="HandleMoveDown"
									  MoveUp="HandleMoveUp"
									  OnDeleted="HandleQuestionDeleted"
									  OnSelected="HandleSelected" />
					}
				</MudStack>
				break;

			case GropType.Grid:
				<MudGrid Spacing="6" xs="3">

					@foreach (var question in QuestionGroupElement.GroupsQuestions)
					{
						<MudItem>
							<QuestionBase QuestionElement="question"
										  IsSelected="question.IsSelected"
										  MoveDown="HandleMoveDown"
										  MoveUp="HandleMoveUp"
										  OnDeleted="HandleQuestionDeleted"
										  OnSelected="HandleSelected" />
						</MudItem>
					}
				</MudGrid>
				break;

			default:
				<MudText Typo="Typo.h6">布局类型: 未知</MudText>
				break;
		}

	</MudPaper>

}
else
{

	@switch (@GropType)
	{
		case GropType.Stack:
			<MudStack Spacing="6">

				@foreach (var question in QuestionGroupElement.GroupsQuestions)
				{
					<QuestionBase QuestionElement="question"
								  IsSelected="question.IsSelected"
								  MoveDown="HandleMoveDown"
								  MoveUp="HandleMoveUp"
								  OnDeleted="HandleQuestionDeleted"
								  OnSelected="HandleSelected" />
				}
			</MudStack>
			break;

		case GropType.Grid:
			<MudGrid Spacing="6" xs="3">

				@foreach (var question in QuestionGroupElement.GroupsQuestions)
				{
					<MudItem>
						<QuestionBase QuestionElement="question"
									  IsSelected="question.IsSelected"
									  MoveDown="HandleMoveDown"
									  MoveUp="HandleMoveUp"
									  OnDeleted="HandleQuestionDeleted"
									  OnSelected="HandleSelected" />
					</MudItem>
				}
			</MudGrid>
			break;

		default:
			<MudText Typo="Typo.h6">布局类型: 未知</MudText>
			break;
	}
}


@code {

	[Parameter]
	public GropType GropType { get; set; } = GropType.Stack;

	[Parameter]
	public bool GroupSelected { get; set; }

	[Parameter]
	public QuestionGroupElement QuestionGroupElement { get; set; }


	[Parameter]
	public bool IsSelected { get; set; }

	private int preSelected = 0;

	protected override void OnInitialized()
	{
		QuestionGroupElement = new QuestionGroupElement();
		QuestionGroupElement.GroupsQuestions = new List<QuestionElement>();
	}

	private void HandleMoveUp(int index)
	{
		if (index >= QuestionGroupElement.GroupsQuestions.Count) return;

		QuestionGroupElement.GroupsQuestions.MoveUp(QuestionGroupElement.GroupsQuestions[index]);
		ReOrderIndex();
		StateHasChanged();
	}

	private void HandleMoveDown(int index)
	{
		if (index >= QuestionGroupElement.GroupsQuestions.Count) return;

		QuestionGroupElement.GroupsQuestions.MoveDown(QuestionGroupElement.GroupsQuestions[index]);
		ReOrderIndex();
		StateHasChanged();
	}

	private void HandleQuestionDeleted(int questionId)
	{
		var questionToRemove = QuestionGroupElement.GroupsQuestions.FirstOrDefault(q => q.Index == questionId);
		if (questionToRemove != null)
		{
			QuestionGroupElement.GroupsQuestions.Remove(questionToRemove);

			StateHasChanged();
		}

		ReOrderIndex();
	}

	private void ReOrderIndex()
	{
		foreach (var que in QuestionGroupElement.GroupsQuestions)
		{
			que.Index = QuestionGroupElement.GroupsQuestions.IndexOf(que);
		}
	}

	public void OnAddText()
	{
		QuestionGroupElement.GroupsQuestions.Add(new QuestionElement { Index = QuestionGroupElement.GroupsQuestions.Count });
		StateHasChanged();
	}


	public void OnAddRadio()
	{
		QuestionGroupElement.GroupsQuestions.Add(new QuestionElement { Index = QuestionGroupElement.GroupsQuestions.Count, QuestionType = BaseQuestionType.Radio });
		StateHasChanged();
	}

	private void HandleClick(MouseEventArgs e)
	{
		HandleSelected(-1);
	}

	private void HandleSelected(int id)
	{
		var ques = QuestionGroupElement.GroupsQuestions.FirstOrDefault(x => x.Index == preSelected);
		if (ques != null) ques.IsSelected = false;

		if (id < 0) return;

		var ques2 = QuestionGroupElement.GroupsQuestions.FirstOrDefault(x => x.Index == id);
		if (ques2 != null) ques2.IsSelected = true;


		preSelected = id;
		StateHasChanged();
	}
}