
/*
Copyright (c) 2012 Cem Kalyoncu

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#include <vector>
#include <string>
#include <Engine/GGEMain.h>
#include <Widgets/Main.h>
#include <Graphics/Canvas.h>
#include <algorithm>
#include <Utils/Random.h>
#include <Widgets/Basic/WidgetLayer.h>
#include <Widgets/WidgetRegistry.h>
#include <Resource/FontTheme.h>

using namespace gge;
using namespace gge::graphics;
using namespace gge::utils;


//color lists, they are not required by the functions, just for reference
static unsigned int bodycolors[]={ 
	0xfff3c3a3,
	0xffffd3b3,
	0xffffe3c3,
	0xfffee3d3,
	0xfff0e8d8,
	0xfff0b393,
	0xffeece84,
	0xffeede84,
	0xff806030,
	0xff604224,
	//0xff306030, //fantasy stuff
	//0xff50e080,
	//0xff804080,
};
static unsigned int eyecolors[]={ 
	0xff493929,
	0xff50ecd4,
	0xff50d4ec,
	0xffb0c084,
};
static unsigned int haircolors[]={ 
	0xff221009,
	0xffa77423,
	0xffb7a423,
	0xffd7a443,
	0xffa74423,
	0xffffeeb0,
};

//clips a value between 0-255 to prevent overflowing
unsigned char clip(double v) {
	return (unsigned char)std::min(std::max(v+0.5,0.),255.);
}

//These are class neutral functions which require the following members of canvas to exist, you may modify the code 
//as necessary
// * int GetWidth() / int GetHeight()
// * T_ operator ()(int x, int y)
// T_ should support lower case ARGB members

//These functions can be implemented using HLS or better yet LCh color modes, however, they will be slower and
//will require additional library. If you do require it, I have the necessary equipment and I will be happy to help.

//Switches body color, both skin and eye. If your model is different than base, you may need to change base colors. 
//Notice that this will work for blue or green eyes, otherwise it would be harder to detect eyes
template<class C_>
void BodyColorSwitch(C_ &canvas, unsigned int bodytarget, unsigned int eyetarget, unsigned int bodybase=0xfff3c3a3, unsigned int eyebase=0xff50d4ec) {
	//extract base color components
	double cr=(bodybase/0x10000)%0x100,cg=(bodybase/0x100)%0x100,cb=(bodybase/0x1)%0x100;
	//calculate old-to-new ratio
	cr=(bodytarget/0x10000)%0x100 / cr;
	cg=(bodytarget/0x100)%0x100 / cg;
	cb=(bodytarget/0x1)%0x100 / cb;

	//same for the eyes
	double ecr=(eyebase/0x10000)%0x100,ecg=(eyebase/0x100)%0x100,ecb=(eyebase/0x1)%0x100;
	ecr=(eyetarget/0x10000)%0x100 / ecr;
	ecg=(eyetarget/0x100)%0x100 / ecg;
	ecb=(eyetarget/0x1)%0x100 / ecb;
	
	//for all pixels, determined by GetHeight and GetWidth functions, change them as you need
	for(int y=0;y<canvas.GetHeight();y++) {
		for(int x=0;x<canvas.GetWidth();x++) {
			//In skin color r>>g and r>>b
			if(canvas(x,y).r>canvas(x,y).g+4 && canvas(x,y).r>canvas(x,y).b+5) {
				canvas(x,y).r=clip(cr*canvas(x,y).r);
				canvas(x,y).g=clip(cg*canvas(x,y).g);
				canvas(x,y).b=clip(cb*canvas(x,y).b);
			}
			//if g>>>r or b>>>r we probably have an eye color
			else if(canvas(x,y).r+10<canvas(x,y).g || canvas(x,y).r+10<canvas(x,y).b) {
				canvas(x,y).r=clip(ecr*canvas(x,y).r);
				canvas(x,y).g=clip(ecg*canvas(x,y).g);
				canvas(x,y).b=clip(ecb*canvas(x,y).b);
			}
			//rest is the white parts of the eyes
		}
	}
}

//This function switches the hair color (can be another part as which will totally be recolored), as before base color is important
//it is designed for the blonde hair wulax had provided
template<class C_>
void HairColorSwitch(C_ &canvas, unsigned int target, unsigned int base=0xffa77423) {
	//same as body color switch except without restrictions
	double cr=(base/0x10000)%0x100,cg=(base/0x100)%0x100,cb=(base/0x1)%0x100;
	cr=(target/0x10000)%0x100 / cr;
	cg=(target/0x100)%0x100 / cg;
	cb=(target/0x1)%0x100 / cb;
	for(int y=0;y<canvas.GetHeight();y++) {
		for(int x=0;x<canvas.GetWidth();x++) {
			canvas(x,y).r=clip(cr*canvas(x,y).r);
			canvas(x,y).g=clip(cg*canvas(x,y).g);
			canvas(x,y).b=clip(cb*canvas(x,y).b);
		}
	}
}


int Application(std::vector<std::string> &args) {
	gge::widgets::InitializeApplication("ColorSwitcher", "ColorSwitcher", 800, 600);

	widgets::WidgetLayer layer;
	Main.Add(layer);

	::graphics::Canvas cnv;
	::graphics::Canvas hr;
	::graphics::Canvas suit;
	::graphics::Canvas bg;

	bool showsuit=true;

	bg.ImportPNG("bg.png");
	bg.Prepare();
	
	layer.MouseCallback.SetLambda([&](input::mouse::Event::Type event, Point location) -> bool {
		if(event==input::mouse::Event::Right_Click)
			showsuit=!showsuit;

		if(input::mouse::Event::isClick(event)) {
			layer.Clear();
			bg.DrawIn(layer);

			unsigned int bodycolor=bodycolors[Random(0,10)];
			unsigned int eyecolor=eyecolors[Random(0,4)];
			unsigned int haircolor=haircolors[Random(0,6)];

			std::stringstream ss;
			ss
				<<"Click to change colors, right click to toggle suit."<<" ... "
				<<std::hex<<bodycolor<<", "<<eyecolor<<", "<<haircolor;
			widgets::WR.Fonts.H1.Theme->DetermineRenderer(gge::Font::H1)->Print(&layer, 0,100, 800, ss.str(), 0xe0000000, TextAlignment::Center);

			cnv.ImportPNG("male_slash.png");
			BodyColorSwitch(cnv, bodycolor, eyecolor);
			cnv.Prepare();
			cnv.DrawIn(layer, SizeController2D::SingleMiddleCenter);

			hr.ImportPNG("hair_blonde.png");
			HairColorSwitch(hr, haircolor);
			hr.Prepare();
			hr.DrawIn(layer, SizeController2D::SingleMiddleCenter);

			suit.ImportPNG("suit.png");
			suit.Prepare();
			if(showsuit)
				suit.DrawIn(layer, SizeController2D::SingleMiddleCenter);

			return true;
		}

		return false;
	});

	layer.MouseCallback.Fire(input::mouse::Event::Left_Click,Point(0,0),1);

	gge::Main.Run();

	return 0;
}
