/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
 */

/** @file transparency_gui.cpp The transparency GUI. */

#include "stdafx.h"
#include "window_gui.h"
#include "transparency.h"
#include "sound_func.h"
#include "settings_type.h"

#include "widgets/transparency_widget.h"

#include "table/sprites.h"
#include "table/strings.h"

#include "safeguards.h"

TransparencyOptions _transparency_opt; ///< The bits that should be transparent.
TransparencyOptions _transparency_lock; ///< Prevent these bits from flipping with X.
TransparencyOptions _invisibility_opt; ///< The bits that should be invisible.
DisplayOptions _display_opt; ///< What do we want to draw/do?
StationFacilities _facility_display_opt; ///< What station facilities to draw.

class TransparenciesWindow : public Window
{
public:
	TransparenciesWindow(WindowDesc &desc, int window_number) : Window(desc)
	{
		this->InitNested(window_number);
	}

	void OnPaint() override
	{
		this->OnInvalidateData(0); // Must be sure that the widgets show the transparency variable changes, also when we use shortcuts.
		this->DrawWidgets();
	}

	/**
	 * Get the \c TransparencyOption associated with a widget.
	 * @param widget the widget.
	 * @return Transparency option associated with the widget.
	 */
	TransparencyOption GetTransparencyOptionOfWidget(WidgetID widget) const
	{
		if (!IsInsideMM(widget, WID_TT_BEGIN, WID_TT_END)) return TransparencyOption::Invalid;
		return static_cast<TransparencyOption>(widget - WID_TT_BEGIN);
	}

	void DrawWidget(const Rect &r, WidgetID widget) const override
	{
		switch (widget) {
			case WID_TT_SIGNS:
			case WID_TT_TREES:
			case WID_TT_HOUSES:
			case WID_TT_INDUSTRIES:
			case WID_TT_BUILDINGS:
			case WID_TT_BRIDGES:
			case WID_TT_STRUCTURES:
			case WID_TT_CATENARY:
			case WID_TT_TEXT: {
				TransparencyOption to = GetTransparencyOptionOfWidget(widget);
				if (_transparency_lock.Test(to)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + WidgetDimensions::scaled.fullbevel.left, r.top + WidgetDimensions::scaled.fullbevel.top);
				break;
			}
			case WID_TT_BUTTONS: {
				const Rect fr = r.Shrink(WidgetDimensions::scaled.framerect);
				for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
					if (i == WID_TT_TEXT) continue; // Loading and cost/income text has no invisibility button.

					const Rect wr = this->GetWidget<NWidgetBase>(i)->GetCurrentRect().Shrink(WidgetDimensions::scaled.fullbevel);
					TransparencyOption to = GetTransparencyOptionOfWidget(i);
					DrawFrameRect(wr.WithY(fr), Colours::PaleGreen, _invisibility_opt.Test(to) ? FrameFlag::Lowered : FrameFlags{});
				}
				break;
			}
		}
	}

	void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
	{
		if (widget >= WID_TT_BEGIN && widget < WID_TT_END) {
			if (_ctrl_pressed) {
				/* toggle the bit of the transparencies lock variable */
				ToggleTransparencyLock(GetTransparencyOptionOfWidget(widget));
				this->SetDirty();
			} else {
				/* toggle the bit of the transparencies variable and play a sound */
				ToggleTransparency(GetTransparencyOptionOfWidget(widget));
				SndClickBeep();
				MarkWholeScreenDirty();
			}
		} else if (widget == WID_TT_BUTTONS) {
			uint i;
			for (i = WID_TT_BEGIN; i < WID_TT_END; i++) {
				const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
				if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
					break;
				}
			}
			if (i == WID_TT_TEXT|| i == WID_TT_END) return;

			ToggleInvisibility(GetTransparencyOptionOfWidget(i));
			SndClickBeep();

			/* Redraw whole screen only if transparency is set */
			if (IsTransparencySet(GetTransparencyOptionOfWidget(i))) {
				MarkWholeScreenDirty();
			} else {
				this->SetWidgetDirty(WID_TT_BUTTONS);
			}
		}
	}

	Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
	{
		Point pt = GetToolbarAlignedWindowPosition(sm_width);
		pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(WID_TT_BUTTONS)->current_y);
		return pt;
	}

	/**
	 * Some data on this window has become invalid.
	 * @param data Information about the changed data.
	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
	 */
	void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
	{
		if (!gui_scope) return;
		for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
			this->SetWidgetLoweredState(i, IsTransparencySet(GetTransparencyOptionOfWidget(i)));
		}
	}
};

static constexpr std::initializer_list<NWidgetPart> _nested_transparency_widgets = {
	NWidget(NWID_HORIZONTAL),
		NWidget(WWT_CLOSEBOX, Colours::DarkGreen),
		NWidget(WWT_CAPTION, Colours::DarkGreen), SetStringTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
		NWidget(WWT_STICKYBOX, Colours::DarkGreen),
	EndContainer(),
	NWidget(NWID_HORIZONTAL),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_SIGNS), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_TREES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_HOUSES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_INDUSTRIES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_BUILDINGS), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_BRIDGES), SetToolbarMinimalSize(2), SetFill(0, 1), SetSpriteTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_STRUCTURES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_CATENARY), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
		NWidget(WWT_IMGBTN, Colours::DarkGreen, WID_TT_TEXT), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_TEXT_TOOLTIP),
		NWidget(WWT_PANEL, Colours::DarkGreen), SetFill(1, 1), EndContainer(),
	EndContainer(),
	/* Panel with 'invisibility' buttons. */
	NWidget(WWT_PANEL, Colours::DarkGreen, WID_TT_BUTTONS), SetMinimalSize(219, 13), SetToolTip(STR_TRANSPARENT_INVISIBLE_TOOLTIP),
	EndContainer(),
};

/** Window definition for the transparency toolbar window. */
static WindowDesc _transparency_desc(
	WindowPosition::Manual, "toolbar_transparency", 0, 0,
	WindowClass::TransparencyToolbar, WindowClass::None,
	{},
	_nested_transparency_widgets
);

/**
 * Show the transparency toolbar.
 */
void ShowTransparencyToolbar()
{
	AllocateWindowDescFront<TransparenciesWindow>(_transparency_desc, 0);
}
