/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.cxf.jaxrs.client;

import java.net.URI;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;

import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.jaxrs.impl.MetadataMap;

/**
 * Keeps the client state such as the baseURI, currentURI, requestHeaders, current response
 *
 */
public class LocalClientState implements ClientState {
    private static final String HTTP_SCHEME = "http";
    
    private MultivaluedMap<String, String> requestHeaders = new MetadataMap<String, String>();
    private MultivaluedMap<String, String> templates;
    private ResponseBuilder responseBuilder;
    private URI baseURI;
    private UriBuilder currentBuilder;
    
    public LocalClientState() {
        
    }
    
    public LocalClientState(URI baseURI) {
        this.baseURI = baseURI;
        String scheme = baseURI.getScheme();
        if (!StringUtils.isEmpty(scheme) && scheme.startsWith(HTTP_SCHEME)) {
            this.currentBuilder = UriBuilder.fromUri(baseURI);
        } else {
            this.currentBuilder = UriBuilder.fromUri("/");
        }
    }
    
    public LocalClientState(LocalClientState cs) {
        this.requestHeaders = new MetadataMap<String, String>(cs.requestHeaders);
        this.templates = cs.templates == null ? null : new MetadataMap<String, String>(cs.templates);
        this.responseBuilder = cs.responseBuilder != null ? cs.responseBuilder.clone() : null;
        this.baseURI = cs.baseURI;
        this.currentBuilder = cs.currentBuilder != null ? cs.currentBuilder.clone() : null;
    }
    
    
    
    public void setCurrentBuilder(UriBuilder currentBuilder) {
        this.currentBuilder = currentBuilder;
    }
    
    public UriBuilder getCurrentBuilder() {
        return currentBuilder;
    }
    
    public void setBaseURI(URI baseURI) {
        this.baseURI = baseURI;
    }
    
    public URI getBaseURI() {
        return baseURI;
    }
    
    public void setResponseBuilder(ResponseBuilder responseBuilder) {
        this.responseBuilder = responseBuilder;
    }
    
    public ResponseBuilder getResponseBuilder() {
        return responseBuilder;
    }
    
    public void setRequestHeaders(MultivaluedMap<String, String> requestHeaders) {
        this.requestHeaders = requestHeaders;
    }
    
    public MultivaluedMap<String, String> getRequestHeaders() {
        return requestHeaders;
    }
    
    public MultivaluedMap<String, String> getTemplates() {
        return templates;
    }

    public void setTemplates(MultivaluedMap<String, String> map) {
        if (templates == null) {
            this.templates = map;
        } else if (map != null) {
            templates.putAll(map);
        } else {
            templates = null;
        }
    }
    
    public void reset() {
        requestHeaders.clear();
        responseBuilder = null;
        currentBuilder = UriBuilder.fromUri(baseURI);
        templates = null;
    }
    
    public ClientState newState(URI newBaseURI, 
                                MultivaluedMap<String, String> headers,
                                MultivaluedMap<String, String> templatesMap) {
        ClientState state = new LocalClientState(newBaseURI);
        if (headers != null) {
            state.setRequestHeaders(headers);
        }
        // we need to carry the template parameters forward
        MultivaluedMap<String, String> newTemplateParams = templates;
        if (newTemplateParams != null && templatesMap != null) {
            newTemplateParams.putAll(templatesMap);
        } else {
            newTemplateParams = templatesMap;
        }
        state.setTemplates(newTemplateParams);
        return state;
    }
}
