//
        // GET: /Invoices/

        public ActionResult Index()
        {
            ApiGetRequest <Invoice> listRequest = new ApiGetRequest <Invoice>
            {
                OrderByClause = "Date DESC",
                WhereClause   = "AmountDue > 0"
            };

            ApiRepository repository = new ApiRepository();
            HttpSessionAccessTokenRepository accessTokenRepository = new HttpSessionAccessTokenRepository(Session);

            if (accessTokenRepository.GetToken("") == null)
            {
                return(new ReturnToHomeResult("There is no access token for the current user. Please click the 'connect' button on the homepage."));
            }

            Response response = repository.Get(accessTokenRepository, listRequest);

            return(View(response.Invoices));
        }
        //
        // GET: /Invoices/

        public ActionResult Index()
        {
            ApiGetRequest<Invoice> listRequest = new ApiGetRequest<Invoice>
            {
                OrderByClause = "Date DESC",
                WhereClause = "AmountDue > 0"
            };

            ApiRepository repository = new ApiRepository();
            HttpSessionAccessTokenRepository accessTokenRepository = new HttpSessionAccessTokenRepository(Session);

            if (accessTokenRepository.GetToken("") == null)
            {
                return new ReturnToHomeResult("There is no access token for the current user. Please click the 'connect' button on the homepage.");
            }

            Response response = repository.Get(accessTokenRepository, listRequest);

            return View(response.Invoices);
        }
        //
        // GET: /Connect/Index - Main OAuth Connection Endpoint

        public ActionResult Index()
        {
            Debug.Write("Processing: /Connect/Index");

            ApiRepository apiRepository = new ApiRepository();
            ITokenRepository <AccessToken>  accessTokenRepository  = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository <RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            // Do we already have a session token in sessionstate? - is it still usable?
            if (accessTokenRepository.GetToken("") != null)
            {
                if (apiRepository.TestConnectionToXeroApi(accessTokenRepository))
                {
                    return(new RedirectResult("~/"));
                }

                // The current session token+secret doesn't work - probably due to it expiring in 30mins.
                accessTokenRepository.SaveToken(null);
            }


            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();
            RequestToken  requestToken = oauthSession.GetRequestToken();

            requestTokenRepository.SaveToken(requestToken);

            Trace.WriteLine("OAuth Request Token: " + requestToken.Token);
            Trace.WriteLine("OAuth Request Secret: " + requestToken.TokenSecret);

            string authorisationUrl = oauthSession.GetUserAuthorizationUrlForToken(requestToken);

            Trace.WriteLine("Redirecting browser to user authorisation uri:" + authorisationUrl);

            return(new RedirectResult(authorisationUrl));
        }
        //
        // GET: /Connect/Index - Main OAuth Connection Endpoint
        
        public ActionResult Index()
        {
            Debug.Write("Processing: /Connect/Index");

            ApiRepository apiRepository = new ApiRepository();
            ITokenRepository<AccessToken> accessTokenRepository = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository<RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            // Do we already have a session token in sessionstate? - is it still usable?
            if (accessTokenRepository.GetToken("") != null)
            {
                if (apiRepository.TestConnectionToXeroApi(accessTokenRepository))
                {
                    return new RedirectResult("~/");
                }

                // The current session token+secret doesn't work - probably due to it expiring in 30mins.
                accessTokenRepository.SaveToken(null);
            }
            

            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();
            RequestToken requestToken = oauthSession.GetRequestToken();

            requestTokenRepository.SaveToken(requestToken);

            Trace.WriteLine("OAuth Request Token: " + requestToken.Token);
            Trace.WriteLine("OAuth Request Secret: " + requestToken.TokenSecret);

            string authorisationUrl = oauthSession.GetUserAuthorizationUrlForToken(requestToken);

            Trace.WriteLine("Redirecting browser to user authorisation uri:" + authorisationUrl);

            return new RedirectResult(authorisationUrl);
        }