/*============================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center (DKFZ)
All rights reserved.

Use of this source code is governed by a 3-clause BSD license that can be
found in the LICENSE file.

============================================================================*/

#include "mitkMultilabelObjectFactory.h"

#include <mitkBaseRenderer.h>
#include <mitkCoreObjectFactory.h>
#include <mitkDataNode.h>
#include <mitkProperties.h>
#include <mitkCoreServices.h>
#include <mitkIPropertyFilters.h>
#include <mitkLabelSetImageVtkMapper2D.h>
#include <mitkMultiLabelSegmentationVtkMapper3D.h>
#include <mitkPropertyFilter.h>

mitk::MultilabelObjectFactory::MultilabelObjectFactory() : CoreObjectFactoryBase()
{
  MITK_DEBUG << "MultilabelObjectFactory c'tor" << std::endl;
}

mitk::MultilabelObjectFactory::~MultilabelObjectFactory()
{
}

mitk::Mapper::Pointer mitk::MultilabelObjectFactory::CreateMapper(mitk::DataNode *node, MapperSlotId id)
{
  mitk::Mapper::Pointer newMapper = nullptr;
  mitk::BaseData *data = node->GetData();

  if (id == mitk::BaseRenderer::Standard2D)
  {
    if ((dynamic_cast<mitk::MultiLabelSegmentation *>(data) != nullptr))
    {
      newMapper = mitk::LabelSetImageVtkMapper2D::New();
      newMapper->SetDataNode(node);
    }
  }
  else if (id == mitk::BaseRenderer::Standard3D)
  {
    if ((dynamic_cast<mitk::MultiLabelSegmentation*>(data) != nullptr))
    {
      newMapper = mitk::MultiLabelSegmentationVtkMapper3D::New();
      newMapper->SetDataNode(node);
    }
  }
  return newMapper;
}

void mitk::MultilabelObjectFactory::SetDefaultProperties(mitk::DataNode *node)
{
  if (node == nullptr)
    return;

  if (node->GetData() == nullptr)
    return;

  if (dynamic_cast<MultiLabelSegmentation *>(node->GetData()) != nullptr)
  {
    mitk::LabelSetImageVtkMapper2D::SetDefaultProperties(node);
    mitk::MultiLabelSegmentationVtkMapper3D::SetDefaultProperties(node);

    auto propertyFilters = CoreServices::GetPropertyFilters();

    if (propertyFilters != nullptr)
    {
      PropertyFilter labelSetImageFilter;
      labelSetImageFilter.AddEntry("binaryimage.hoveringannotationcolor", PropertyFilter::Blacklist);
      labelSetImageFilter.AddEntry("binaryimage.hoveringcolor", PropertyFilter::Blacklist);
      labelSetImageFilter.AddEntry("binaryimage.selectedannotationcolor", PropertyFilter::Blacklist);
      labelSetImageFilter.AddEntry("binaryimage.selectedcolor", PropertyFilter::Blacklist);
      labelSetImageFilter.AddEntry("outline binary shadow color", PropertyFilter::Blacklist);

      propertyFilters->AddFilter(labelSetImageFilter, "MultiLabelSegmentation");
    }
  }
}

struct RegisterMultilabelObjectFactory
{
  RegisterMultilabelObjectFactory() : m_Factory(mitk::MultilabelObjectFactory::New())
  {
    mitk::CoreObjectFactory::GetInstance()->RegisterExtraFactory(m_Factory);
  }

  ~RegisterMultilabelObjectFactory() { mitk::CoreObjectFactory::GetInstance()->UnRegisterExtraFactory(m_Factory); }
  mitk::MultilabelObjectFactory::Pointer m_Factory;
};

static RegisterMultilabelObjectFactory registerMultilabelObjectFactory;
