namespace DocuMed.PWA.Pages;

public class MedicalHistoryActionPageViewModel : BaseViewModel<MedicalHistoryLDto>
{
    private readonly IRestWrapper _restWrapper;
    private readonly NavigationManager _navigationManager;
    private readonly ISnackbar _snackbar;
    private readonly IUserUtility _userUtility;

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

    public MedicalHistoryTemplateSDto SelectedTemplate { get; set; } = new();
    public MedicalHistoryTemplateLDto SelectedTemplateLDto { get; set; } = new();

    public bool IsEditing { get; set; } = false;

    public SectionSDto? SelectedSelection { get; set; }
    public List<MedicalHistoryAnswerSDto> PiAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> PdhAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> PshAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> FhAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> DhAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> HhAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> GaAnswers { get; set; } = new();
    public List<MedicalHistoryAnswerSDto> RosAnswers { get; set; } = new();

    public MudCarousel<object>? Carousel { get; set; }
    public int CurrentStep { get; set; } = 0;
    public string StepCounter { get; set; } = "1 / 5";
    public bool MedicalHistorySubmitted { get; set; } = false;
    public async Task CompleteStepClicked()
    {
        CurrentStep++;
        StepCounter = $"{CurrentStep + 1} / 5";
        if (CurrentStep == 1)
            await CheckMedicalHistoryAsync();
        if (CurrentStep == 5)
        {
            //if (IsEditing)
            //    await EditTemplateAsync();
            //else
                await SubmitMedicalHistoryAsync();
            MedicalHistorySubmitted = true;
        }
        Carousel?.MoveTo(CurrentStep);
    }

    private async Task CheckMedicalHistoryAsync()
    {
        try
        {
            if (SelectedTemplate.Id == Guid.Empty)
                return;

            IsProcessing = true;
            var token = await _userUtility.GetBearerTokenAsync();
            var dto = await _restWrapper.CrudDtoApiRest<MedicalHistoryTemplateSDto, MedicalHistoryTemplateLDto, Guid>(Address.MedicalHistoryTemplateController)
                .ReadOne(SelectedTemplate.Id, token);
            
            PageDto.Id = dto.Id;
            SelectedTemplateLDto = dto;
        }
        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 async Task SubmitMedicalHistoryAsync()
    {
        try
        {
            IsProcessing = true;
            if (SelectedSelection == null)
                throw new Exception("لطفا بخش مورد نظر را انتخاب نمایید");
            PageDto.SectionId = SelectedSelection.Id;
            var token = await _userUtility.GetBearerTokenAsync();
            PageDto.Answers.AddRange(PiAnswers);
            PageDto.Answers.AddRange(PdhAnswers);
            PageDto.Answers.AddRange(PshAnswers);
            PageDto.Answers.AddRange(FhAnswers);
            PageDto.Answers.AddRange(DhAnswers);
            PageDto.Answers.AddRange(HhAnswers);
            PageDto.Answers.AddRange(GaAnswers);
            PageDto.Answers.AddRange(RosAnswers);
            await _restWrapper.CrudDtoApiRest<MedicalHistoryLDto, MedicalHistorySDto, Guid>(Address.MedicalHistoryController)
                .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 CompleteCreateMedicalHistory()
    {
        _navigationManager.NavigateTo("HomePage");
    }

}