/*                                              -*- mode:C++ -*-
  Element_Aura.h an element attracts medicine particles
  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_Aura.h Basic integer holding element
  \author Xinyu Chen
  \date (C) 2014 All rights reserved.
  \lgpl
 */
#ifndef ELEMENT_AURA_H
#define ELEMENT_AURA_H

#include "Element.h"
#include "ColorMap.h"
#include "EventWindow.h"
#include "ElementTable.h"
#include "itype.h"
#include "P3Atom.h"
#include "Logger.h"
namespace MFM
{

#define AURA_VERSION 1

  template <class CC>
  class Element_Aura : public Element<CC>
  {
    typedef typename CC::ATOM_TYPE T;
    typedef typename CC::PARAM_CONFIG P;
    enum {
	   R = P::EVENT_WINDOW_RADIUS,
	   BITS = P::BITS_PER_ATOM,

	   DENSE_POS = P3Atom<P>::P3_STATE_BITS_POS,
	   DENSE_LEN = 16,
	   ID_POS = DENSE_POS + DENSE_LEN,
	   ID_LEN = 16,
	   LIFE_POS = ID_POS + ID_LEN,
	   LIFE_LEN = 8
    };

    typedef BitField<BitVector<BITS>, VD::U32, DENSE_LEN, DENSE_POS> AFDense;
    typedef BitField<BitVector<BITS>, VD::U32, ID_LEN, ID_POS> AFId;
    typedef BitField<BitVector<BITS>, VD::U32, LIFE_LEN, LIFE_POS> AFLife;

  private:


  public:
    u16 GetDense(const T& us) const
    {
      return AFDense::Read(this->GetBits(us));
    }

    void SetDense(T& us, const u16 dense) const
    {
      AFDense::Write(this->GetBits(us), dense);
    }
    u16 GetID(const T& us) const
    {
      return AFId::Read(this->GetBits(us));
    }

    void SetID(T& us, const u16 timer) const
    {
      AFId::Write(this->GetBits(us), timer);
    }
    u8 GetLife(const T& us) const
    {
      return AFLife::Read(this->GetBits(us));
    }

    void SetLife(T& us, const u8 timer) const
    {
      AFLife::Write(this->GetBits(us), timer);
    }


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

    Element_Aura() : Element<CC>(MFM_UUID_FOR("Signal", AURA_VERSION))
    {
      Element<CC>::SetAtomicSymbol("Sg");
      Element<CC>::SetName("Signal");
    }


    virtual const T & GetDefaultAtom() const
    {
      static T defaultAtom(TYPE(),0,0,0);
      SetDense(defaultAtom, 0);
      SetID(defaultAtom, 0);
      return defaultAtom;
    }

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

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

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

    virtual const char* GetDescription() const
    {
      return "An element designed simply to attract Request "
             "emitted by an Provider.";
    }
    virtual void AppendDescription(const T* atomPtr, OString64& desc) const
    {
      const T& atom = *atomPtr;

      u16 d = GetDense(atom);
      u16 myid= GetID(atom);
      desc.Printf("den(%d) id(%d)", d,myid);
    }
   /*******************************
     * catch medicine *
     *******************************/
    virtual bool CatchMedicine(EventWindow<CC>& window) const;
   /*******************************
     * check neighbor aura density *
     *******************************/
    virtual void ChkDen(EventWindow<CC>& window) const;

    /*******************************
     * main behaviors
     *******************************/

    virtual void Behavior(EventWindow<CC>& window) const;

  };

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

}
#include "Element_Aura.tcc"
#endif /* ELEMENT_AURA_H */
