/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2013 University of California, Los Angeles
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
 *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
 */
#include "ccnx-key.h"
#include <tinyxml.h>
#include <boost/lexical_cast.hpp>
#include "logging.h"

INIT_LOGGER ("Ccnx.Key");

using namespace std;

namespace Ccnx {

Key::Key()
    : m_meta("", "",  0, 0)
{
}

Key::Key(const PcoPtr &keyObject, const PcoPtr &metaObject = PcoPtr())
    : m_meta("", "", 0, 0)
{
  m_name = keyObject->name();
  m_raw = keyObject->content();
  m_hash = *(Hash::FromString(string((const char *)head(m_raw), m_raw.size())));
  updateMeta(metaObject);
}

void
Key::updateMeta(const PcoPtr &metaObject)
{
  if (metaObject)
  {
    TiXmlDocument doc;
    Bytes xml = metaObject->content();
    // just make sure it's null terminated as it's required by TiXmlDocument::parse
    xml.push_back('\0');
    doc.Parse((const char *)(head(xml)));
    if (!doc.Error())
    {
      TiXmlElement *root = doc.RootElement();
      for (TiXmlElement *child = root->FirstChildElement(); child; child = child->NextSiblingElement())
      {
        const char *elemName = child->Value();
        const char *text = child->GetText();
        if (elemName == "Name")
        {
          m_meta.realworldID = text;
        }
        else if (elemName == "Affiliation")
        {
          m_meta.affiliation = text;
        }
        else if (elemName == "Valid_to")
        {
          m_meta.validTo = boost::lexical_cast<time_t>(std::string(text));
        }
        else if (elemName == "Valid_from")
        {
          // this is not included in the key meta yet
          // but it should eventually be there
        }
        else
        {
          // ignore known stuff
        }
      }
    }
    else
    {
      _LOG_ERROR("Cannot parse meta info:" << std::string(head(xml), xml.size()));
    }
  }
}

Key::VALIDITY
Key::validity()
{
  if (m_meta.validFrom == 0 && m_meta.validTo == 0)
  {
    return OTHER;
  }

  time_t now = time(NULL);
  if (now < m_meta.validFrom)
  {
    return NOT_YET_VALID;
  }

  if (now >= m_meta.validTo)
  {
    return EXPIRED;
  }

  return VALID;
}

} // Ccnx
