//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     This library is free software; you can redistribute it and/or
//     modify it under the terms of the GNU Lesser General Public License
//     as published by the Free Software Foundation; either version 2.1
//     of the License, or any later version.
//
//     This library is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//     GNU Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General
//     Public License along with this library; if not, write to the
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
//     Boston, MA 02111-1307 USA
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.4 (15127), 2011/06/03 14:52:49
//                        $Revision: 14936 $
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.response;

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

import de.knipp.rritoolkit.KVList;
import de.knipp.rritoolkit.KVMultiList;
import de.knipp.rritoolkit.ResponseParseException;

import static de.knipp.rritoolkit.DenicConstants.NS_DOMAIN;

import static de.knipp.rritoolkit.Utils.parseDateTime;

import static de.knipp.rritoolkit.XmlHelper.getFirstElement;
import static de.knipp.rritoolkit.XmlHelper.getFirstElementText;



/**
 *  response of a AuthInfo 1 create request
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class AuthInfo1CreateResponse extends Response
{

  /** the actual expiration date */

  private Date expirationDate;



  //--- implementation of Response ---------------------------------------------


  /**
   *  subclasses that need to read additional data from the key-value pair
   *  response should override this method. The default implementation is empty
   *  and need not to be called.
   *
   *  @param list                     the list
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */

  @Override
  protected void dataFromList (KVMultiList list)
    throws ResponseParseException
  {
    if (isSuccess ())
    {
      KVList main = list.getMainSection ();
      String temp = main.getFirst ("authinfoexpire");

      // this is is a work-around of a DENIC-bug which will be removed again
      // in future versions. The bug manifests in a date returned by DENIC
      // not containing the seconds in it.

      if (temp.length () == 22)
      {
        Pattern datePattern = Pattern.compile (
          "(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})" +
          "([+\\-])(\\d{2}):(\\d{2})");

        Matcher m = datePattern.matcher (temp);

        if (m.matches ())
        {
          Calendar cal = new GregorianCalendar ();
          cal.clear ();

          cal.set (Integer.parseInt (m.group (1)),
            Integer.parseInt (m.group (2)) - 1,
            Integer.parseInt (m.group (3)), Integer.parseInt (m.group (4)),
            Integer.parseInt (m.group (5)), 0);

          int offset = Integer.parseInt (m.group (7)) * 3600000 +
            Integer.parseInt (m.group (8)) * 60000;

          if (m.group (6).equals ("-")) offset = -offset;
          cal.set (Calendar.ZONE_OFFSET, offset);

          expirationDate = cal.getTime ();
          return;
        }
      }

      // end of work-around

      expirationDate = parseDateTime (temp);
    }
  }

  /**
   *  subclasses that need to read additional data from the XML response should
   *  override this method. The default implementation is empty and need not
   *  to be called.
   *
   *  @param data                     the data element or {@code null}
   *                                  if the data contains no additional data
   *  @throws DOMException            on DOM exceptions
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */

  @Override
  protected void dataFromXml (Element data)
    throws DOMException, ResponseParseException
  {
    if (data != null && isSuccess ())
    {
      Element el = getFirstElement (data, NS_DOMAIN, "createAuthInfo1Data");

      if (el != null)
      {
        String temp = getFirstElementText (el, NS_DOMAIN, "expire");
        expirationDate = parseDateTime (temp);
      }
    }
  }

  /**
   *  call the suitable method of the given visitor
   *
   *  @param visitor               the visitor
   */

  @Override
  public void accept (ResponseVisitor visitor)
  {
    visitor.visit (this);
  }




  /**
   *  return a string representation for debugging purposes
   *
   *  @return                      the string representation
   */

  @Override
  public String toString ()
  {
    return super.toString () + "  expiration date: " + expirationDate + "\n";
  }

  //--- access methods ---------------------------------------------------------


  /**
   *  return the actual expiration date that has been set by the registry
   *
   *  @return               the date, may be {@code null} if the request failed
   */

  public Date getExpirationDate ()
  {
    return expirationDate;
  }
}

