/*
 * Copyright 2019 Arcus Project
 *
 * Licensed 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 com.iris.billing.deserializer;

import javax.xml.parsers.SAXParser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.iris.billing.client.model.Subscription;
import com.iris.billing.client.model.SubscriptionAddon;

class SubscriptionAddonDeserializer extends AbstractRecurlyDeserializer<SubscriptionAddon> {
   private SubscriptionAddon addon;

   public SubscriptionAddonDeserializer() {
      this(null);
   }

   SubscriptionAddonDeserializer(SAXParser saxParser) {
      super(saxParser, SubscriptionAddon.Tags.TAG_NAME);
      addon = new SubscriptionAddon();
   }

   @Override
   protected SubscriptionAddon getResult() {
      return addon;
   }
   
   @Override
   protected void onReturnFrom(AbstractRecurlyDeserializer<?> childDeserializer) {
      // No child elements to worry about.  
   }

   @Override
   public void onStartElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
      if (SubscriptionAddon.Tags.TAG_NAME.equals(qName)) {
         addon = new SubscriptionAddon();
      }
   }

   @Override
   public void onEndElement (String uri, String localName, String qName) throws SAXException {
      switch(qName) {
         case SubscriptionAddon.Tags.ADD_ON_CODE:
            addon.setAddonCode(getCurrentTextValue());
            break;
         case Subscription.Tags.UNIT_AMOUNT_IN_CENTS:
            addon.setUnitAmountInCents(getCurrentTextValue());
            break;
         case Subscription.Tags.QUANTITY:
            addon.setQuantity(Integer.valueOf(getCurrentTextValue()));
            break;
         default:
            // No-Op
            break;
      }
   }
}

