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> GaQuestions { get; set; } = new();
    public List<MedicalHistoryQuestionSDto> RosQuestions { 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 async Task InitializeAsync()
    {
        if (_templateId != Guid.Empty && IsEditing)
        {
            try
            {
                IsProcessing = true;
                var token = await _userUtility.GetBearerTokenAsync();
                var dto = await _restWrapper.CrudDtoApiRest<MedicalHistoryTemplateSDto, MedicalHistoryTemplateLDto, Guid>(Address.MedicalHistoryTemplateController)
                    .ReadOne(_templateId, token);
                PageDto.ApplicationUserId = dto.ApplicationUserId;
                PageDto.ChiefComplaint = dto.ChiefComplaint;
                PageDto.SectionId = dto.SectionId;
                PageDto.Id = dto.Id;
                SelectedSelection = dto.Section;
                PiQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PresentIllness));
                PdhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PastDiseasesHistory));
                PshQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.PastSurgeryHistory));
                FhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.FamilyHistory));
                DhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.DrugHistory));
                AhQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.AddictionHistory));
                GaQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.GeneralAppearance));
                RosQuestions.AddRange(dto.Questions.Where(q => q.Part == MedicalHistoryPart.ReviewOfSystem));
            }
            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;
            }
        }
        await base.InitializeAsync();
    }

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

    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(PshQuestions);
            PageDto.Questions.AddRange(FhQuestions);
            PageDto.Questions.AddRange(DhQuestions);
            PageDto.Questions.AddRange(AhQuestions);
            PageDto.Questions.AddRange(GaQuestions);
            PageDto.Questions.AddRange(RosQuestions);
            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 async Task EditTemplateAsync()
    {
        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(PshQuestions);
            PageDto.Questions.AddRange(FhQuestions);
            PageDto.Questions.AddRange(DhQuestions);
            PageDto.Questions.AddRange(AhQuestions);
            PageDto.Questions.AddRange(GaQuestions);
            PageDto.Questions.AddRange(RosQuestions);
            await _restWrapper.CrudDtoApiRest<MedicalHistoryTemplateLDto, MedicalHistoryTemplateSDto, Guid>(Address.MedicalHistoryTemplateController)
                .Update(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");
    }
}