namespace DocuMed.PWA.Pages;

public class MedicalHistoryTemplateActionPageViewModel : BaseViewModel<MedicalHistoryTemplateLDto>
{
    private readonly NavigationManager _navigationManager;
    private readonly ISnackbar _snackbar;
    private readonly IRestWrapper _restWrapper;
    private readonly IUserUtility _userUtility;
    private readonly Guid _templateId;
    public MudCarousel<object>? Carousel { get; set; }
    public bool MedicalHistorySubmitted { get; set; }
    public int CurrentStep { get; set; } = 0;
    public string StepCounter { get; set; } = "1 / 5";
    public bool IsEditing { get; set; } = false;

    public List<MedicalHistoryQuestionSDto> PiQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> PdhQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> PshQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> FhQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> DhQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> AhQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> VsQuestions { get; set; } = new();
    public SectionSDto? SelectedSelection { get; set; }

    


    public MedicalHistoryTemplateActionPageViewModel(
        NavigationManager navigationManager,
        ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility)
    {
        _navigationManager = navigationManager;
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
    }

    public MedicalHistoryTemplateActionPageViewModel(
        NavigationManager navigationManager,
        ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility,
        Guid templateId)
    {
        _navigationManager = navigationManager;
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _templateId = templateId;
        IsEditing = true;
    }

    public override Task InitializeAsync()
    {
        return base.InitializeAsync();
    }

    public async Task CompleteStepClicked()
    {
        Carousel?.MoveTo(++CurrentStep);
        StepCounter = $"{CurrentStep + 1} / 5";
        if (CurrentStep == 5)
        {
            MedicalHistorySubmitted = true;
            await SubmitTemplateAsync();
        }
    }

    public async Task SubmitTemplateAsync()
    {
        try
        {
            IsProcessing = true;
            if (SelectedSelection == null)
                throw new Exception("لطفا بخش مورد نظر را انتخاب نمایید");
            PageDto.SectionId = SelectedSelection.Id;
            var token = await _userUtility.GetBearerTokenAsync();
            PageDto.SectionId = SelectedSelection.Id;
            PageDto.Questions.AddRange(PiQuestions);
            PageDto.Questions.AddRange(PdhQuestions);
            PageDto.Questions.AddRange(PiQuestions);
            PageDto.Questions.AddRange(PshQuestions);
            PageDto.Questions.AddRange(FhQuestions);
            PageDto.Questions.AddRange(DhQuestions);
            PageDto.Questions.AddRange(AhQuestions);
            PageDto.Questions.AddRange(VsQuestions);
            await _restWrapper.CrudDtoApiRest<MedicalHistoryTemplateLDto, MedicalHistoryTemplateSDto, Guid>(Address.MedicalHistoryTemplateController)
                .Create(PageDto, token);

        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }
    }
    public void RollBackStepClicked()
    {
        Carousel?.MoveTo(--CurrentStep);
        StepCounter = $"{CurrentStep + 1} / 5";
    }

    public void SubmitCreateTemplateAsync()
    {
        _navigationManager.NavigateTo("HomePage");
    }
}