using TEAMModelOS.SDK;

using System;
using System.Collections.Generic;
using System.Text;

namespace TEAMModelOS.SDK
{
    
    public  class ResponseBuilder
    {
        private string message="Success";
        private object data;
        private long total;
        private int currPage;
        private int pageSize;
        private int totalPage;
        private Dictionary<string, object> extend;
        private Pagination page;
        private AzureTableToken token;
        private ErrorModel error =null;
        
        public ResponseBuilder()
        {
        }
        public ResponseBuilder Success()
        {
            error = null;
            return this;
        }

        public ResponseBuilder Success(String message)
        {
            this.message = message;
            return this;
        }
        public static ResponseBuilder custom()
        {
            return new ResponseBuilder();
        }

        public ResponseBuilder Data(object data)
        {
            this.data = data;
            return this;
        }
		public ResponseBuilder Error( int code, string message)
		{
			
			this.error = new ErrorModel { code=code, message=message, data = null };
			return this;
		}
        public ResponseBuilder Error(int code, Dictionary<string,object> errorData)
        {
           
            this.error = new ErrorModel { code = code, message = message ,data= errorData };
            return this;
        }
        public ResponseBuilder Error( int code)
		{

            this.error = new ErrorModel { code = code, message = "Error", data = null };
            return this;
		}
        public ResponseBuilder Error(int code, string message, Dictionary<string, object> errorData)
        {

            this.error = new ErrorModel { code = code, message = message, data = errorData };
            return this;
        }

        public ResponseBuilder Extend(Dictionary<String, object> extend)
        {
            this.extend = extend;
            return this;
        }
        public ResponseBuilder Token(AzureTableToken token)
        {
            this.token = token;
            return this;
        }
        public ResponseBuilder Page(Pagination page)
        {
            this.pageSize = page.pageSize;
            this.currPage = page.currPage;
            this.total = page.total;
            this.page = page;
            this.totalPage = (int)Math.Ceiling((double)this.total / (double)this.pageSize);
            return this;
        }
        public ResponseBuilder totalCount(int totalCount)
        {
            this.total = totalCount;
            return this;
        }

        public ResponseBuilder CurrPage(int currPage)
        {
            this.currPage = currPage;
            return this;
        }

        public ResponseBuilder PageSize(int pageSize)
        {
            this.pageSize = pageSize;
            return this;
        }

        public ResponseBuilder TotalPage(int totalPage)
        {
            this.totalPage = totalPage;
            return this;
        }
        public BaseResponse build()
        {

            object baseResponse = null;
            if (error != null) {
                ErrorJosnResponse errorJosnRPCResponse = new ErrorJosnResponse();
                errorJosnRPCResponse.error = error.data;
                errorJosnRPCResponse.code = error.code;
                errorJosnRPCResponse.message = error.message;
                return errorJosnRPCResponse;
            }
            if (this.total > 0 && this.pageSize > 0)
            {
                this.totalPage = (int)Math.Ceiling((double)this.total / (double)this.pageSize);
            }
            if (null != this.data && this.token != null)
            {
                TokenJosnResponse<object> response = new TokenJosnResponse<object>();
                response.result.data = this.data;
                response.result.extend = this.extend;
                response.result.azureToken = this.token;
                response.code = 0;
                response.message = message;

                baseResponse = response;
            }
            else if (null != this.data && this.total > 0 && this.currPage > 0 && this.pageSize > 0 && this.totalPage > 0)
            {
                PageJosnResponse<object> response = new PageJosnResponse<object>();
                response.result.data = this.data;
                response.result.page = new Pagination(this.total, this.currPage, this.pageSize, this.totalPage);
                response.result.extend = this.extend;
                response.message = message;
                response.code = 0;
                baseResponse = response;
            }
            else if (this.data != null)
            {
                DataJsonResponse<object> response = new DataJsonResponse<object>();
                response.result.data = this.data;
                response.result.extend = this.extend;
                response.message = message;
                response.code = 0;
                baseResponse = response;
            }
            else if (this.data == null) {
                DataJsonResponse<object> response = new DataJsonResponse<object>();
                response.result.data = this.data;
                response.result.extend = this.extend;
                response.message = message;
                response.code = 0;
                baseResponse = response;
            }
            else
            {
                return new EmptyJosnResponse() ;
            }
            return (BaseResponse)baseResponse;
        }
         
    }
}
