using System;
using System.Net;
using ServiceStack;
using Newtonsoft.Json;
using StubCore.Entities;
using Newtonsoft.Json.Linq;
using WebService.ServiceModel;
using System.Collections.Generic;
using StubCore.Plugins.QueryOperations;

namespace WebService.ServiceInterface
{
    public class FlexibleService : Service
    {
        /*
        /// <summary>
        /// Flexible Query Service for POST method only.
        /// </summary>
        /// <param name="_request">Request object with data</param>
        /// <returns>Response with message or error</returns>
        /// <remarks>
        /// Last changed: 08.10.2015
        /// Author: Mikhael Aktsyzniy
        /// </remarks>
        public object Post(FlexibleQueryRequest _request)
        {
            string _requestType = base.Request.ContentType;
            var _streamContext = String.Empty;

            if(!String.IsNullOrEmpty(_request.RequestStream.ToString()))
            {
                foreach(string line in _request.RequestStream.ReadLines())
                {
                    _streamContext += line;
                }
            }      

            //var _data = new FlexQueryData(_request.Profile, _request.Method, _requestType, _streamContext);



            OperationResponse _response = _data.InitResponse();

            if(_response.isCompleted)
            {

                if (!String.IsNullOrEmpty(_data._response._redirectUrl))
                {
                    return new HttpResult() { StatusCode = _data._response._statusCode, Headers = { { HttpHeaders.Location, _data._response._redirectUrl } } };
                }

                var _result = new object();

                if (_data._response._jsonResponse != null && !String.IsNullOrEmpty(_data._response._jsonResponse.ToString()))
                {

                    _result = _data._response._jsonResponse.ToString();
                }
                else
                {
                    _result = _data._response._xmlResponse;
                }

                return new HttpResult(_result, _data._response._contentType, _data._response._statusCode)
                {
                    StatusDescription = _response.message
                };    
            }

            return new HttpError(HttpStatusCode.Conflict, _response.message);
        }

        public object Get(FlexibleQueryGetRequest _request)
        {

            var _data = new FlexQueryData(_request.Profile, _request.FileResponse + "." + _request.Format);
            OperationResponse _response = _data.InitGetResponse();

            if (_response.isCompleted)
            {

                object _result = _data._response._jsonResponse.ToString();

                if (String.IsNullOrEmpty(_data._response._jsonResponse.ToString()))
                {
                    _result = _data._response._xmlResponse;
                }

                return new HttpResult(_result, _data._response._contentType, _data._response._statusCode)
                {
                    StatusDescription = _response.message
                };
            }
            return new HttpError(HttpStatusCode.Conflict, _response.message);
        }
    
        */
    }
}
