/*
 * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     rbarkhouse - 2.4 - initial implementation
 package org.eclipse.persistence.testing.jaxb.dynamic.util;

import java.util.StringTokenizer;

import jakarta.xml.bind.annotation.adapters.XmlAdapter;

import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;

public class LinkAdapter extends XmlAdapter<Object, DynamicEntity> {

    // ValueType = mynamespace.Link
    // BoundType = mynamespace.Address

    public static DynamicJAXBContext jc = null;

    public LinkAdapter() {
        this(jc);
    }

    public LinkAdapter(DynamicJAXBContext jc) {
        LinkAdapter.jc = jc;
    }

    @Override
    public DynamicEntity unmarshal(Object v) throws Exception {
        DynamicEntity link = (DynamicEntity) v;

        DynamicEntity address = jc.newDynamicEntity("mynamespace.Address");

        StringTokenizer tokenizer = new StringTokenizer(link.get("href").toString(), "&", false);

        String street = tokenizer.nextToken();
        street = street.substring(street.lastIndexOf("=") + 1);
        street = street.replace('_', ' ');
        String city = tokenizer.nextToken();
        city = city.substring(city.lastIndexOf("=") + 1);
        String state = tokenizer.nextToken();
        state = state.substring(state.lastIndexOf("=") + 1);
        String zip = tokenizer.nextToken();
        zip = zip.substring(zip.lastIndexOf("=") + 1);

        address.set("street", street);
        address.set("city", city);
        address.set("state", state);
        address.set("zip", zip);

        return address;
    }

    @Override
    public Object marshal(DynamicEntity v) throws Exception {
        if(null == jc || null == v) {
            return null;
        }

        String street = v.get("street");
        street = street.replace(' ', '_');
        String city = v.get("city");
        String state = v.get("state");
        String zip = v.get("zip");

        DynamicEntity link = jc.newDynamicEntity("mynamespace.Link");
        link.set("rel", "self");
        link.set("href", "http://www.address.com/findAddress?str=" + street + "&city=" + city + "&state=" + state + "&zip=" + zip);
        return link;
    }

}
