/*------------------------------------------------------------------------------
 src/bin/djview/imageOpStack.cxx

 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djImageOpStack.h"
#include <djImageApp.h>

namespace dj {

ImageOpStack::ImageOpStack()
{
  DJ_DBG_FNC("dj::ImageOpStack::ImageOpStack");
}

ImageOpStack::~ImageOpStack()
{
  DJ_DBG_FNC("dj::ImageOpStack::~ImageOpStack");
  
  for (int i = 0; i < _stackList.size(); ++i)
    for (ListIt<ImageOp *> it = _stackList[i]; it.isValid(); ++it)
      delete it();
}

void ImageOpStack::stackNew(const Str & name)
{
  DJ_DBG_FNC("dj::ImageOpStack::stackNew");
  DJ_DBG_PRN("name = " << name);
  
  _nameList += name;
  _stackList += List<ImageOp *>();
}

bool ImageOpStack::stackCopy(int in)
{
  if (in < 0 || in >= _stackList.size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::stackCopy");
  DJ_DBG_PRN("in= " << in);
  
  _nameList += _nameList[in];

  const List<ImageOp *> & stack = _stackList[in];
  List<ImageOp *> stackNew;
  for (ConstListIt<ImageOp *> it = stack; it.isValid(); ++it)
    stackNew += static_cast<ImageOp *>(it()->copy());
  _stackList += stackNew;
  
  return true;
}

bool ImageOpStack::stackDel(int in)
{
  if (in < 0 || in >= _stackList.size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::stackDel");
  DJ_DBG_PRN("in = " << in);

  List<ImageOp *> & stack = _stackList[in];
  for (ListIt<ImageOp *> it = stack; it.isValid(); ++it)
    delete it();
  _nameList.del(in);
  _stackList.del(in);
  
  return true;
}

bool ImageOpStack::stackName(const Str & name, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::stackName");
  DJ_DBG_PRN("name = " << name);
  DJ_DBG_PRN("stack = " << stack);
  
  _nameList[stack] = name;
  
  return true;
}

bool ImageOpStack::stackLeft(int in)
{
  if (in <= 0 || in >= _stackList.size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::stackLeft");
  DJ_DBG_PRN("in = " << in);
  
  _nameList.insert(_nameList.del(in), in - 1);
  _stackList.insert(_stackList.del(in), in - 1);
    
  return true;
}

bool ImageOpStack::stackRight(int in)
{
  if (in < 0 || in >= _stackList.size() - 1) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::stackRight");
  DJ_DBG_PRN("in = " << in);
  
  _nameList.insert(_nameList.del(in), in + 1);
  _stackList.insert(_stackList.del(in), in + 1);
  
  return true;
}

bool ImageOpStack::opNew(const Str & name, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::opNew");
  DJ_DBG_PRN("name = " << name);
  DJ_DBG_PRN("stack = " << stack);
  
  ImageOp * op = static_cast<ImageOp *>(djImageApp->imageOp().pluginCopy(name));
  if (! op) return false;
  _stackList[stack] += op;
  
  return true;
}

bool ImageOpStack::opCopy(int in, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  if (in < 0 || in >= _stackList[stack].size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::opCopy");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("stack = " << stack);
  
  _stackList[stack] += static_cast<ImageOp *>(_stackList[stack][in]->copy());
  
  return true;
}

bool ImageOpStack::opDel(int in, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  if (in < 0 || in >= _stackList[stack].size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::opDel");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("stack = " << stack);
  
  delete _stackList[stack].del(in);
  
  return true;
}

bool ImageOpStack::opLeft(int in, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  if (in < 0 || in >= _stackList[stack].size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::opLeft");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("stack = " << stack);
  
  _stackList[stack].insert(_stackList[stack].del(in), in - 1);
  
  return true;
}

bool ImageOpStack::opRight(int in, int stack)
{
  if (stack < 0 || stack >= _stackList.size()) return false;
  if (in < 0 || in >= _stackList[stack].size()) return false;
  
  DJ_DBG_FNC("dj::ImageOpStack::opRight");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("stack = " << stack);
  
  _stackList[stack].insert(_stackList[stack].del(in), in + 1);
  
  return true;
}

}
