/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2024 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
 */

#ifndef DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA_H_
#define DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA_H_

#include "drc_re_base_constraint_data.h"

/**
 * Simple constraint data object used by custom rules.  These rules do not have
 * any structured parameters; instead the full rule text is stored here.
 */
class DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA : public DRC_RE_BASE_CONSTRAINT_DATA
{
public:
    DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA() = default;

    DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA( int aId, int aParentId, const wxString& aRuleName ) :
            DRC_RE_BASE_CONSTRAINT_DATA( aId, aParentId, aRuleName )
    {
    }

    explicit DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA( const DRC_RE_BASE_CONSTRAINT_DATA& aBaseData ) :
            DRC_RE_BASE_CONSTRAINT_DATA( aBaseData )
    {
    }

    wxString GetRuleText() const { return m_ruleText; }

    void SetRuleText( const wxString& aText ) { m_ruleText = aText; }

    wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) override
    {
        if( m_ruleText.IsEmpty() )
            return wxEmptyString;

        wxString ruleName = aContext.ruleName;
        ruleName.Replace( wxS( "\"" ), wxS( "\\\"" ) );

        wxString rule;
        rule << wxS( "(rule \"" ) << ruleName << wxS( "\"\n" );

        if( !aContext.comment.IsEmpty() )
        {
            wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );

            for( const wxString& line : lines )
            {
                if( line.IsEmpty() )
                    continue;

                rule << wxS( "\t# " ) << line << wxS( "\n" );
            }
        }

        rule << m_ruleText << wxS( ")" );

        return rule;
    }

    VALIDATION_RESULT Validate() const override
    {
        VALIDATION_RESULT result;

        if( m_ruleText.IsEmpty() )
            result.AddError( _( "Rule text cannot be empty" ) );

        return result;
    }

    void CopyFrom( const ICopyable& aSource ) override
    {
        const auto& src = dynamic_cast<const DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA&>( aSource );
        DRC_RE_BASE_CONSTRAINT_DATA::CopyFrom( src );
        m_ruleText = src.m_ruleText;
    }

private:
    wxString m_ruleText;
};

#endif // DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA_H_
