using ESuite.UI.E2E.Helpers;

namespace ESuite.UI.E2E.StepDefinitions
{
    [Binding]
    public class MailCheckStepDefinitions
    {
        private readonly ScenarioContext _scenarioContext;
        private readonly APIHelper apiHelper;
        private readonly ConfigHelper configHelper;

        public MailCheckStepDefinitions(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
            apiHelper = new APIHelper(_scenarioContext);
            configHelper = new ConfigHelper();
        }

        [When(@"I click on the Link in the sent email")]
        public async Task WhenIClickOnTheLinkInTheSentEmail()
        {
            var token = await apiHelper.GetAuthToken();
            string url = GetUrl("ConfirmEmailAddress");
            string confirmationLink = await apiHelper.SendAuthenticatedRequest(url, token);
            I.NavigateToURL(confirmationLink);
        }

        [When(@"I click on the Link in the reset password email")]
        public async Task WhenIClickOnTheLinkInTheResetPasswordEmail()
        {
            var token = await apiHelper.GetAuthToken();
            string url = GetUrl("PasswordReset");
            string confirmationLink = await apiHelper.SendAuthenticatedRequest(url, token);

            I.NavigateToURL(confirmationLink);
        }

        private string GetUrl(string emailUserActionType)
        {
            var uriBuilder = new UriBuilder($"{configHelper.APIUrl}/api/User/currentEmailUserActionUrl")
            {
                Query = $"emailAddress={_scenarioContext["UserEmail"]}&emailUserActionType={emailUserActionType}"
            };
            return uriBuilder.ToString();
        }
    }
}