/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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 3
 * 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, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <widgets/properties_panel.h>
#include <set>

class SELECTION;
class SCHEMATIC;
class SCH_BASE_FRAME;
class SCH_COMMIT;
class SCH_EDIT_FRAME;
class SCH_SHEET;
class PROPERTY_MANAGER;
class PG_UNIT_EDITOR;
class PG_CHECKBOX_EDITOR;
class PG_COLOR_EDITOR;
class PG_FPID_EDITOR;
class PG_URL_EDITOR;

class SCH_PROPERTIES_PANEL : public PROPERTIES_PANEL
{
public:
    SCH_PROPERTIES_PANEL( wxWindow* aParent, SCH_BASE_FRAME* aFrame );

    virtual ~SCH_PROPERTIES_PANEL();

    void UpdateData() override;

    void AfterCommit() override;

protected:
    void rebuildProperties( const SELECTION& aSelection ) override;
    wxPGProperty* createPGProperty( const PROPERTY_BASE* aProperty ) const override;
    bool getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue ) override;

    PROPERTY_BASE* getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const;

    void valueChanging( wxPropertyGridEvent& aEvent ) override;
    void valueChanged( wxPropertyGridEvent& aEvent ) override;

    bool handleSheetFilenameChange( SCH_EDIT_FRAME* aFrame, SCH_SHEET* aSheet,
                                    SCH_COMMIT& aChanges, const wxString& aNewFilename );

    void OnLanguageChanged( wxCommandEvent& aEvent ) override;

    /**
     * Get the current selection from the selection tool.
     * If the selection is empty and we're in the symbol editor, returns the current symbol instead.
     *
     * @param aFallbackSelection [out] local SELECTION object for fallback symbol selection
     * @return const SELECTION& reference to the selection (either real selection or fallback)
     */
    const SELECTION& getSelection( SELECTION& aFallbackSelection );

    /**
     * Get the front item of the current selection.
     * If the selection is empty and we're in the symbol editor, returns the current symbol instead.
     *
     * @return EDA_ITEM* pointer to the front item, or nullptr if no selection
     */
    EDA_ITEM* getFrontItem();

protected:
    SCH_BASE_FRAME*     m_frame;
    PROPERTY_MANAGER&   m_propMgr;
    PG_UNIT_EDITOR*     m_unitEditorInstance;
    PG_CHECKBOX_EDITOR* m_checkboxEditorInstance;
    PG_COLOR_EDITOR*    m_colorEditorInstance;
    PG_FPID_EDITOR*     m_fpEditorInstance;
    PG_URL_EDITOR*      m_urlEditorInstance;

    static std::set<wxString> m_currentSymbolFieldNames;
    static std::set<wxString> m_currentSheetFieldNames;
    wxPGChoices               m_nets;
};
