/*                                              -*- mode:C++ -*-
  Element_Cell_Core.h Core cell element
  Copyright (C) 2014 The Regents of the University of New Mexico.  All rights reserved.

  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 (at your option) 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 General Public License
  along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  USA
*/

/**
  \file Element_Cell_Core.h Core cell element
  \author Trent R. Small.
  \author Xinu Chen.
  \date (C) 2014 All rights reserved.
  \lgpl
 */
#ifndef ELEMENT_CELL_CORE_H
#define ELEMENT_CELL_CORE_H

#include "Element.h"
#include "EventWindow.h"
#include "ElementTable.h"
#include "itype.h"
#include "P3Atom.h"
#include "Element_Cell_Wall.h"

namespace MFM
{

#define CELL_VERSION 1

  template <class CC>
  class Element_Cell_Core : public Element<CC>
  {
    // Extract short names for parameter types
    typedef typename CC::ATOM_TYPE T;
    typedef typename CC::PARAM_CONFIG P;
    enum
    {
      R = P::EVENT_WINDOW_RADIUS,
      BITS = P::BITS_PER_ATOM,

      TIMER_POS = P3Atom<P>::P3_STATE_BITS_POS,
      TIMER_LEN = 32
    };

    typedef BitField<BitVector<BITS>, TIMER_LEN, TIMER_POS> AFTimer;

    u32 GetTimer(const T& us) const
    {
      return AFTimer::Read(this->GetBits(us));
    }

    void SetTimer(T& us, const u32 timer) const
    {
      AFTimer::Write(this->GetBits(us), timer);
    }

    /**
     * returns true if timer == 0
     */
    bool DecrementTimer(T& us) const
    {
      u32 timerVal = GetTimer(us);
      if(timerVal == 0)
      {
	timerVal = 1;
      }
      SetTimer(us, timerVal - 1);

      return timerVal == 1;
    }

  private:

    ElementParameterS32<CC> m_timerAge;

  public:

    static Element_Cell_Core THE_INSTANCE;
    static const u32 TYPE()
    {
      return THE_INSTANCE.GetType();
    }

    Element_Cell_Core()
      : Element<CC>(MFM_UUID_FOR("CellCore", CELL_VERSION)),
	m_timerAge(this, "timerAge", "Maximum Cell Age", "Maximum Cell Age",
		   1, 200, 5000, 1)
    {
      Element<CC>::SetAtomicSymbol("Cc");
      Element<CC>::SetName("CellCore");
    }

    virtual const T & GetDefaultAtom() const
    {
      static T defaultAtom(TYPE(),0,0,0);

      SetTimer(defaultAtom, m_timerAge.GetValue());

      return defaultAtom;
    }

    virtual u32 PercentMovable(const T& you,
                               const T& me, const SPoint& offset) const
    {
      return 0;
    }

    virtual u32 DefaultPhysicsColor() const
    {
      return 0xff00ff00;
    }

    virtual u32 DefaultLowlightColor() const
    {
      return 0xff007f00;
    }

    virtual void Behavior(EventWindow<CC>& window) const
    {
      const T& us = window.GetCenterAtom();
      T copyUs = window.GetCenterAtom();
      MDist<R>& md = MDist<R>::get();

      if(GetTimer(us) == (u32)m_timerAge.GetValue())
      {
	for(u32 i = md.GetFirstIndex(1); i <= md.GetLastIndex(1); i++)
	{
	  SPoint loc = md.GetPoint(i);
	  T wall = Element_Cell_Wall<CC>::THE_INSTANCE.GetDefaultAtom();

	  Element_Cell_Wall<CC>::THE_INSTANCE.SetTimer(wall, GetTimer(us));
	  Element_Cell_Wall<CC>::THE_INSTANCE.SetLoc(wall, loc);

	  window.SetRelativeAtom(loc, wall);
	}
      }

      if(DecrementTimer(copyUs))
      {
	window.SetCenterAtom(Element_Empty<CC>::THE_INSTANCE.GetDefaultAtom());
      }
      else
      {
	window.SetCenterAtom(copyUs);
      }
    }
  };

  template <class CC>
  Element_Cell_Core<CC> Element_Cell_Core<CC>::THE_INSTANCE;

}

#endif /* ELEMENT_CELL_CORE_H */
