@rendermode InteractiveServer

<MudPaper @onclick="HandleClick" Outlined="true" Elevation="@(IsSelected ? 8 : 2)" Class="ma-5 pa-2">

	@if (IsSelected)
	{
		<MudPaper Elevation="0" Class="my-2">

			<MudIconButton Icon="@Icons.Material.Filled.MoveUp" Color="Color.Success" Size="Size.Small" OnClick="HandleMoveUp" />
			<MudIconButton Icon="@Icons.Material.Filled.MoveDown" Color="Color.Success" Size="Size.Small" OnClick="HandleMoveDown" />
			<MudIconButton Icon="@Icons.Material.Filled.Delete" Color="Color.Error" Size="Size.Small" OnClick="HandleDelete" />


			<MudDivider />



			<MudStack Row="true">

				<MudText> @QuestionGroupElement.Number </MudText>
				<MudTextField @bind-Value=QuestionGroupElement.Title></MudTextField>
			</MudStack>
			<MudDivider />
			<MudTextField Label="Descript" @bind-Value=QuestionGroupElement.Descript AutoGrow></MudTextField>


		</MudPaper>
	}
	else
	{

		<MudStack>

			<MudStack Row="true">
				<MudText> @QuestionGroupElement.Number </MudText>

				<MudText Typo="Typo.h6">@QuestionGroupElement.Title</MudText>
			</MudStack>
			<MudDivider />
			@if (!string.IsNullOrEmpty(QuestionGroupElement.Descript))
			{

				<MudTextField ReadOnly="true" @bind-Value=QuestionGroupElement.Descript AutoGrow></MudTextField>
			}
		</MudStack>

	}

	@switch (QuestionGroupElement.QuestionType)
	{
		case QuestionType.Spelling:
			<CommonGroup QuestionGroupElement="QuestionGroupElement" IsSelected="QuestionGroupElement.IsSelected" GroupSelected="IsSelected" />
			break;
		default:
			<MudText Color="Color.Warning">未知或未实现的编辑器类型: @QuestionGroupElement.QuestionType</MudText>
			@if (IsSelected)
			{
				<MudText Typo="Typo.body2">选中此题，但无对应编辑器可编辑内容。</MudText>
			}
			break;
	}
	<MudDivider />
</MudPaper>

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

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

	[Parameter]
	public EventCallback<int> OnSelected { get; set; }

	[Parameter]
	public EventCallback<int> OnDeleted { get; set; }

	[Parameter]
	public EventCallback<int> MoveUp { get; set; }

	[Parameter]
	public EventCallback<int> MoveDown { get; set; }


	private async Task HandleClick()
	{
		if (!IsSelected)
		{
			await OnSelected.InvokeAsync(QuestionGroupElement.Number);
		}
	}

	private async Task HandleDelete()
	{
		await OnDeleted.InvokeAsync(QuestionGroupElement.Number);
	}

	private async Task HandleMoveUp()
	{
		await MoveUp.InvokeAsync(QuestionGroupElement.Number);
	}

	private async Task HandleMoveDown()
	{
		await MoveDown.InvokeAsync(QuestionGroupElement.Number);
	}


	private string GetQuestionTypeName(QuestionType type)
	{
		return type switch
		{
			QuestionType.Spelling => "拼写题",
			QuestionType.Pronunciation => "读音选择题",
			QuestionType.WordFormation => "组词题",
			QuestionType.FillInTheBlanks => "选词填空/补充词语",
			QuestionType.SentenceDictation => "默写句子",
			QuestionType.SentenceRewriting => "仿句/改写",
			QuestionType.ReadingComprehension => "阅读理解",
			QuestionType.Composition => "作文题",
			_ => "未知类型"
		};
	}
}
