/*
 * @file llpanelprofilelegacy.h
 * @brief Legacy protocol avatar profile panel
 *
 * Copyright (c) 2014, Cinder Roxley <cinder@sdf.org>
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef LL_PANELPROFILELEGACY_H
#define LL_PANELPROFILELEGACY_H

#include "llgroupmgr.h"
#include "llpanelavatar.h"
#include "llclassifieditem.h"

class LLAvatarName;
class LLFlatListView;
class LLPanel;
class LLPanelPickEdit;
class LLPanelPickInfo;
class LLPanelProfileTab;
class LLPanelClassifiedInfo;
class LLPanelClassifiedEdit;
class LLPickItem;
class LLTextBase;
class LLToggleableMenu;

class LLPanelProfileLegacy final : public LLPanelProfileTab
{
public:
	LLPanelProfileLegacy();
	BOOL postBuild() override;
	void onOpen(const LLSD& key) override;
	void reshape(S32 width, S32 height, BOOL called_from_parent = TRUE) override;
	
protected:
	void openPanel(LLPanel* panel, const LLSD& params);
	void closePanel(LLPanel* panel);
	
private:
	~LLPanelProfileLegacy();
	void updateData() override;
	void processProperties(void* data, EAvatarProcessorType type) override;
	void resetControls() override;
	void setProgress(bool started);
	void showAccordion(const std::string& name, bool show);
	void onAvatarNameCache(const LLUUID& agent_id, const LLAvatarName& av_name);
	void onCommitAvatarProperties();
	void onCommitInterest();
	void onCommitNotes(LLUICtrl* ctrl);
	void onDoubleClickName();
	void onCommitRights();
	void onBackBtnClick();
	void onCommitModifyObjectsRights(LLUICtrl* ctrl);
	void onCommitAction(const LLSD& userdata);
	void copyData(const LLSD& userdata);
	void onNameChanged();
	bool isActionEnabled(const LLSD& userdata);
	bool handleConfirmModifyRightsCallback(const LLSD& notification, const LLSD& response);
	void closeParentFloater();
	
	boost::signals2::connection mAvatarNameCacheConnection;
	boost::signals2::connection mNameChangedConnection;
	
	class ChildStack
	{
	public:
		ChildStack();
		~ChildStack();
		void setParent(LLPanel* parent);
		
		bool push();
		bool pop();
		void preParentReshape();
		void postParentReshape();
		
	private:
		void dump();
		
		typedef LLView::child_list_t view_list_t;
		typedef std::list<view_list_t> stack_t;
		
		stack_t		mStack;
		stack_t		mSavedStack;
		LLPanel*	mParent;
	};
	ChildStack		mChildStack;
	
public:
	class LLPanelProfilePicks final : public LLPanelProfileTab
	{
		friend class LLPanelProfileLegacy;
	public:
		LLPanelProfilePicks();
		~LLPanelProfilePicks();
		BOOL postBuild() override;

	protected:
		void onOpen(const LLSD& key) override;

	private:
		void updateData() override;
		void processProperties(void* data, EAvatarProcessorType type) override;
		void resetControls() override {};
		void showAccordion(const std::string& name, bool show);
		void setProfilePanel(LLPanelProfileLegacy* profile_panel);
		LLPanelProfileLegacy* getProfilePanel() const;
		void onPanelPickClose(LLPanel* panel);
		void onPanelPickSave(LLPanel* panel);
		void onPanelPickEditSave(LLPanelPickEdit* panel);
		void onPanelEdit();
		void onPanelClassifiedEdit();
		void onPanelPickEdit();
		void onPlusMenuItemClicked(const LLSD& param);
		void createNewPick();
		void createNewClassified();
		void editClassified(const LLUUID& classified_id);
		void updateButtons() override;
		void onClickPlusBtn();
		void onClickInfo();
		void onClickTeleport();
		void onClickShowOnMap();
		void onClickDelete();
		void openPickInfo();
		void openClassifiedInfo();
		void onPanelClassifiedSave(LLPanelClassifiedEdit* panel);
		void onPanelClassifiedClose(LLPanelClassifiedInfo* panel);
		void onDoubleClickClassifiedItem(LLUICtrl* item);
		LLClassifiedItem* findClassifiedById(const LLUUID& classified_id) const;
		LLClassifiedItem* getSelectedClassifiedItem() const;
		LLPickItem* getSelectedPickItem() const;

		void createPickEditPanel();
		void createClassifiedEditPanel(LLPanelClassifiedEdit** panel);

		bool isActionEnabled(const LLSD& userdata) const;
		bool callbackDeletePick(const LLSD& notification, const LLSD& response);
		bool callbackDeleteClassified(const LLSD& notification, const LLSD& response);
		bool callbackTeleport(const LLSD& notification, const LLSD& response);
		virtual void onRightMouseUpItem(LLUICtrl* item, S32 x, S32 y, MASK mask);
		
		LLPanelProfileLegacy* mProfilePanel;
		LLFlatListView* mClassifiedsList;
		LLFlatListView* mPicksList;
		LLPanelPickEdit* mPanelPickEdit;
		LLPanelPickInfo* mPanelPickInfo;
		LLPanelClassifiedInfo* mPanelClassifiedInfo;
		LLHandle<LLView> mPopupMenuHandle;
		LLHandle<LLToggleableMenu> mPlusMenuHandle;

		// This map is needed for newly created classifieds. The purpose of panel is to
		// sit in this map and listen to LLPanelClassifiedEdit::processProperties callback.
		panel_classified_edit_map_t mEditClassifiedPanels;
	};
	
	class LLPanelProfileGroups final : public LLPanelProfileTab
	{
		friend class LLPanelProfileLegacy;
	public:
		LLPanelProfileGroups();
		BOOL postBuild() override;

	protected:
		void onOpen(const LLSD& key) override;

	private:
		void updateData() override;
		void processProperties(void* data, EAvatarProcessorType type) override;
		void resetControls() override {};
		void showGroup(const LLUUID& id);
		
		LLTextBase* mGroupsText;
		LLFlatListView* mGroupsList;
	};
	
private:
	LLPanelProfilePicks* mPanelPicks;
	LLPanelProfileGroups* mPanelGroups;
};

class LLProfileGroupItem final : public LLPanel, public LLGroupMgrObserver
{
public:
	LLProfileGroupItem();
	~LLProfileGroupItem();
	static LLProfileGroupItem* create();
	void init(const LLAvatarGroups::LLGroupData& data);
	BOOL postBuild() override;
	
	void setValue(const LLSD& value) override;
	void setId(const LLUUID& id);
	void setInsignia(const LLUUID& id);
	void setGroupName(const std::string& name);
	void setCharter(const std::string& charter);

protected:
	void changed(LLGroupChange gc) override;

private:
	LLUUID	mInsignia;
	std::string mGroupName;
	std::string mCharter;	
};

#endif //LL_PANELPROFILELEGACY_H
