// GET: GreenCardRequests
        public async Task <ActionResult> Index(int page = 0, SDK.DTO.GreenCard.GreenCardRequestStatus status = SDK.DTO.GreenCard.GreenCardRequestStatus.All)
        {
            if (string.IsNullOrWhiteSpace(SessionManager.BearerToken))
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (page < 0)
            {
                page = 0;
            }

            Stopwatch watch = Stopwatch.StartNew();

            // retrieves the list of requests : note that this is for demo purpose,
            // in real life application you should keep the correlationid of the greencard request and retrieve the response when the user needs it
            using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
            }))
            {
                try
                {
                    var requests = await client.GetRequests(page, 50, new System.Threading.CancellationToken(), status);

                    watch.Stop();
                    ViewBag.Time = watch.ElapsedMilliseconds;
                    return(View(requests));
                }
                catch (AssurBoxException aex)
                {
                    ViewBag.Errors = GetError(aex);
                }
            }
            return(View());
        }
        // GET: GreenCardRequests/Create
        public async Task <ActionResult> Create()
        {
            if (string.IsNullOrWhiteSpace(SessionManager.BearerToken))
            {
                return(RedirectToAction("Index", "Home"));
            }

            try
            {
                using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                    Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
                }))
                {
                    ViewBag.Insurances = await client.GetInsurancesGreenCardsIssuers();
                }
            }
            catch (AssurBoxException aex)
            {
                ModelState.AddModelError("", aex.Message);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }


            var model = FakeData.GetGreenCardRequest();

            return(View(model));
        }
        public async Task <FileResult> DownloadSNCADoc(Guid id)
        {
            using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
            }))
            {
                var snca = await client.GetDocumentSNCA(id);

                return(File(Convert.FromBase64String(snca.Content), snca.Type, snca.Filename));
            }
        }
        public async Task <ActionResult> Create(GreenCardRequestInitialization model)
        {
            if (string.IsNullOrWhiteSpace(SessionManager.BearerToken))
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (ModelState.IsValid)
            {
                using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                    Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
                }))
                {
                    try
                    {
                        var creationResult = await client.NewRequest(model);

                        return(RedirectToAction("Details", new { id = creationResult.ResponseContent }));
                    }
                    catch (AssurBoxException ex)
                    {
                        ModelState.AddModelError("", "An error occured " + ex.Message + " code " + ex.ErrorCode);
                        try
                        {
                            if (ex.ErrorDetail != null)
                            {
                                ModelState.AddModelError("", ex?.ErrorDetail?.Error?.Message);
                                foreach (var err in ex.ErrorDetail.Error.Details)
                                {
                                    ModelState.AddModelError("", err.Message);
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("", ex?.ErrorDetail?.Error?.Message);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
            }))
            {
                ViewBag.Insurances = await client.GetInsurancesGreenCardsIssuers();
            }
            return(View(model));
        }
        // GET: GreenCardRequests/Details/5
        public async Task <ActionResult> Details(Guid id)
        {
            if (string.IsNullOrWhiteSpace(SessionManager.BearerToken))
            {
                return(RedirectToAction("Index", "Home"));
            }
            using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
            }))
            {
                var request = await client.GetRequest(id);

                SaveFilesInTempDir(request);

                return(View(request));
            }
        }
        public async Task <ActionResult> Delete(GreenCardRequestCancellation model)
        {
            if (string.IsNullOrWhiteSpace(SessionManager.BearerToken))
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (ModelState.IsValid)
            {
                using (SDK.Clients.CarGreenCardClient client = new SDK.Clients.CarGreenCardClient(new AssurBoxClientOptions {
                    Environment = AssurBoxEnvironments.Test, ApiKey = SessionManager.BearerToken
                }))
                {
                    try
                    {
                        var creationResult = await client.CancelRequest(model);

                        return(RedirectToAction("Index"));
                    }
                    catch (AssurBoxException ex)
                    {
                        ModelState.AddModelError("", "An error occured " + ex.Message);
                        try
                        {
                            ModelState.AddModelError("", ex.ErrorDetail.Error.Message);
                            foreach (var err in ex.ErrorDetail.Error.Details)
                            {
                                ModelState.AddModelError("", err.Message);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return(View(model));
        }