#include "gosGml.hpp"
#include "Scrollbar.h"

Scrollbar::Scrollbar(int Right, int Top, int Height, int textHeight, DWORD upPic, DWORD downPic, DWORD thumbPic, DWORD gutterPic)
:TextHeight(textHeight),ThumbLock(false),ScrollAmount(0),ThumbArea(0),UpPic(upPic),DownPic(downPic),ThumbPic(thumbPic),GutterPic(gutterPic),Enabled(false)
{
	if(GetImageWidth(UpPic)>GetImageWidth(DownPic))
		if(GetImageWidth(GutterPic)>GetImageWidth(ThumbPic))
			if(GetImageWidth(GutterPic)>GetImageWidth(UpPic))
				Width=GetImageWidth(GutterPic);
			else
				Width=GetImageWidth(UpPic);
		else
			if(GetImageWidth(ThumbPic)>GetImageWidth(UpPic))
				Width=GetImageWidth(ThumbPic);
			else
				Width=GetImageWidth(UpPic);
	else
		if(GetImageWidth(GutterPic)>GetImageWidth(ThumbPic))
			if(GetImageWidth(GutterPic)>GetImageWidth(DownPic))
				Width=GetImageWidth(GutterPic);
			else
				Width=GetImageWidth(DownPic);
		else
			if(GetImageWidth(ThumbPic)>GetImageWidth(DownPic))
				Width=GetImageWidth(ThumbPic);
			else
				Width=GetImageWidth(DownPic);
	CenterX=Right-(Width>>1);
	TopButtonArea=new rect;
	TopButtonArea->Right=CenterX+(GetImageWidth(UpPic)>>1);
	TopButtonArea->Left=CenterX-(GetImageWidth(UpPic)>>1);
	TopButtonArea->Top=Top;
	TopButtonArea->Bottom=Top+GetImageHeight(UpPic);
	BottomButtonArea=new rect;
	BottomButtonArea->Top=Top+Height-GetImageHeight(DownPic);
	BottomButtonArea->Left=CenterX-(GetImageWidth(DownPic)>>1);
	BottomButtonArea->Bottom=Top+Height;
	BottomButtonArea->Right=CenterX+(GetImageWidth(DownPic)>>1);
	GutterArea=new rect;
	GutterArea->Top=Top+GetImageHeight(UpPic);
	GutterArea->Left=CenterX-(GetImageWidth(GutterPic)>>1);
	GutterArea->Bottom=Top+Height-GetImageHeight(DownPic);
	GutterArea->Right=CenterX+(GetImageWidth(GutterPic)>>1);
	ScrollBarArea=new rect;
	ScrollBarArea->Top=Top;
	ScrollBarArea->Left=Right-Width;
	ScrollBarArea->Bottom=Top+Height;
	ScrollBarArea->Right=Right;
	ThumbArea=new rect;
	ThumbArea->Top=(BottomButtonArea->Top+TopButtonArea->Bottom)>>1;
	ThumbArea->Left=CenterX-(GetImageWidth(ThumbPic)>>1);
	ThumbArea->Bottom=ThumbArea->Top;
	ThumbArea->Right=CenterX+(GetImageWidth(ThumbPic)>>1);
}

Scrollbar::~Scrollbar()
{
	delete TopButtonArea;
	delete BottomButtonArea;
	delete GutterArea;
	delete ScrollBarArea;
	delete ThumbArea;
}

bool Scrollbar::MouseDown()
{
	return ThumbLock;
}

rect* Scrollbar::GetSBArea()
{
	return ScrollBarArea;
}

int Scrollbar::Mouse(bool LMButtonDown, int x, int y)
{
	if(!Enabled)
		return 0;
	if(ThumbLock)
	{
		if(LMButtonDown)
		{
			ThumbArea->Top+=y-LastMouseY;
			LastMouseY=y;
			if(ThumbArea->Top<TopButtonArea->Bottom)
				ThumbArea->Top=TopButtonArea->Bottom;
			else
				if(ThumbArea->Top+ThumbHeight>BottomButtonArea->Top)
					ThumbArea->Top=BottomButtonArea->Top-ThumbHeight;
			ScrollAmount=ScrollAmountFromThumb();
			return ScrollAmount;
		}
		else
		{
			ThumbLock=false;
			return ScrollAmount;
		}
	}
	else
	{
		if(LMButtonDown)
			if(WithinArea(x,y,ScrollBarArea))
				if(WithinArea(x,y,TopButtonArea))
				{
					ScrollAmount-=6;
					if(ScrollAmount<0)
						ScrollAmount=0;
				}
				else
					if(WithinArea(x,y,BottomButtonArea))
					{
						ScrollAmount+=6;
						if(ScrollAmount>TextHeight-ScrollBarArea->Bottom+ScrollBarArea->Top)
							ScrollAmount=TextHeight-ScrollBarArea->Bottom+ScrollBarArea->Top;
					}
					else
						if(WithinArea(x,y,ThumbArea))
						{
							ThumbLock=true;
							LastMouseY=y;
						}
						else
							if(WithinArea(x,y,GutterArea))
							{
								if(y>ThumbArea->Bottom)
								{
									ScrollAmount+=Height;
									if(ScrollAmount>TextHeight-ScrollBarArea->Bottom+ScrollBarArea->Top)
										ScrollAmount=TextHeight-ScrollBarArea->Bottom+ScrollBarArea->Top;
								}
								else
									if(y<ThumbArea->Top)
									{
										ScrollAmount-=Height;
										if(ScrollAmount<0)
											ScrollAmount=0;
									}
							}
		return ScrollAmount;
	}
}

void Scrollbar::ReCalc(int textHeight)
{
	if(textHeight!=TextHeight-ScrollAmount)
	{
		ScrollAmount=0;
		TextHeight=textHeight;
		ThumbArea->Top=TopButtonArea->Bottom;
	}
	if(ScrollBarArea->Bottom-ScrollBarArea->Top >= TextHeight)
	{
		Enabled=false;
		return;
	}
	Enabled=true;
	ThumbHeight=((ScrollBarArea->Bottom-ScrollBarArea->Top)*(BottomButtonArea->Top-TopButtonArea->Bottom))/TextHeight;
	ThumbArea->Left=TopButtonArea->Left;
	ThumbArea->Bottom=ThumbArea->Top+ThumbHeight;
	ThumbArea->Right=TopButtonArea->Right;
}

bool Scrollbar::IsEnabled()
{
	return Enabled;
}

void Scrollbar::Draw()
{
	if(!Enabled)
		return;
	DrawDImage(UpPic,TopButtonArea);
	DrawDImage(DownPic,BottomButtonArea);
	DrawDImage(GutterPic,GutterArea);
	DrawDImage(ThumbPic,ThumbArea);
}

int Scrollbar::ScrollAmountFromThumb()
{
	return ((ThumbArea->Top-TopButtonArea->Bottom)*TextHeight)/(BottomButtonArea->Top-TopButtonArea->Bottom);
}

int Scrollbar::ThumbFromScrollAmount()
{
	return ((ScrollAmount*(BottomButtonArea->Top-TopButtonArea->Bottom))/TextHeight)+TopButtonArea->Bottom;
}

int Scrollbar::GetWidth()
{
	return Width;
}
