/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
 * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file aperture_macro.cpp
 */


#include <gerbview.h>
#include <aperture_macro.h>
#include <gerber_draw_item.h>


void APERTURE_MACRO::AddPrimitiveToList( AM_PRIMITIVE& aPrimitive )
{
    m_primitivesList.push_back( aPrimitive );
    m_primitivesList.back().m_LocalParamLevel = m_localParamStack.size();
}

void APERTURE_MACRO::AddLocalParamDefToStack()
{
    m_localParamStack.push_back( AM_PARAM() );
}


AM_PARAM& APERTURE_MACRO::GetLastLocalParamDefFromStack()
{
    return m_localParamStack.back();
}


SHAPE_POLY_SET* APERTURE_MACRO::GetApertureMacroShape( const GERBER_DRAW_ITEM* aParent,
                                                       const VECTOR2I&         aShapePos )
{
    SHAPE_POLY_SET holeBuffer;

    m_shape.RemoveAllContours();
    D_CODE * dcode = aParent->GetDcodeDescr();

    for( AM_PRIMITIVE& prim_macro : m_primitivesList )
    {
        if( prim_macro.m_Primitive_id == AMP_COMMENT )
            continue;

        if( prim_macro.IsAMPrimitiveExposureOn( dcode ) )
        {
            prim_macro.ConvertBasicShapeToPolygon( dcode, m_shape );
        }
        else
        {
            prim_macro.ConvertBasicShapeToPolygon( dcode, holeBuffer );

            if( holeBuffer.OutlineCount() )     // we have a new hole in shape: remove the hole
            {
                m_shape.BooleanSubtract( holeBuffer, SHAPE_POLY_SET::PM_FAST );
                holeBuffer.RemoveAllContours();
            }
        }
    }

    // Merge and cleanup basic shape polygons
    m_shape.Simplify( SHAPE_POLY_SET::PM_FAST );

    // A hole can be is defined inside a polygon, or the polygons themselve can create
    // a hole when merged, so we must fracture the polygon to be able to drawn it
    // (i.e link holes by overlapping edges)
    m_shape.Fracture( SHAPE_POLY_SET::PM_FAST );

    // Move m_shape to the actual draw position:
    for( int icnt = 0; icnt < m_shape.OutlineCount(); icnt++ )
    {

        SHAPE_LINE_CHAIN& outline = m_shape.Outline( icnt );

        for( int jj = 0; jj < outline.PointCount(); jj++ )
        {
            VECTOR2I point = outline.CPoint( jj );
            point += aShapePos;
            point = aParent->GetABPosition( point );
            outline.SetPoint( jj, point );
        }
    }

    return &m_shape;
}


double APERTURE_MACRO::GetLocalParam( const D_CODE* aDcode, unsigned aParamId ) const
{
    // find parameter descr.
    const AM_PARAM * param = nullptr;

    for( unsigned ii = 0; ii < m_localParamStack.size(); ii ++ )
    {
        if( m_localParamStack[ii].GetIndex() == aParamId )
        {
            param = &m_localParamStack[ii];
            break;
        }
    }

    if ( param == nullptr )    // not found
        return 0.0;

    // Evaluate parameter
    double value = param->GetValue( aDcode );

    return value;
}
