package zero1hd.rhythmbullet.desktop;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar.ProgressBarStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox.SelectBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;

import zero1hd.rhythmbullet.util.AssetPack;

public class DesktopAssetPack implements AssetPack {
	private FreeTypeFontGenerator default_fontGenerator;
	private FreeTypeFontGenerator darktech_ldr_fontGenerator;
	
	@Override
	public void initiate() {
		default_fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Gasalt-Regular.ttf"));
		darktech_ldr_fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/darktech_ldr.ttf"));
	}
	
	@Override
	public void queueTextures(AssetManager assetManager) {
		assetManager.load("uiskin.atlas", TextureAtlas.class);
		assetManager.load("Tech-Circle1.png", Texture.class);
		assetManager.load("polyjet-standard.png", Texture.class);
		assetManager.load("keyboard.atlas", TextureAtlas.class);
		assetManager.load("cybercircle3B.png", Texture.class);
		assetManager.load("title.png", Texture.class);
		assetManager.load("cybercircle1.png", Texture.class);
		assetManager.load("defaultCover.png", Texture.class);
		assetManager.load("laser.png", Texture.class);
		assetManager.load("pellet.png", Texture.class);
		assetManager.load("shard.png", Texture.class);
		assetManager.load("bar.png", Texture.class);
		assetManager.load("flake.png", Texture.class);
		assetManager.load("void_circle.png", Texture.class);
		assetManager.load("tpSelector.png", Texture.class);
		assetManager.load("backgrounds/mainBG.png", Texture.class);
		assetManager.load("magic1.png", Texture.class);
	}

	@Override
	public void queueSFX(AssetManager assetManager) {
		assetManager.load("pop_open.ogg", Sound.class);
		assetManager.load("pop_close.ogg", Sound.class);
		assetManager.load("laser.ogg", Sound.class);
		assetManager.load("explosion.ogg", Sound.class);
		assetManager.load("disintegrate.ogg", Sound.class);
	}

	@Override
	public void queueParticles(AssetManager assetManager) {
		assetManager.load("standard_thrust.p", ParticleEffect.class);
		assetManager.load("teleport-cloak.p", ParticleEffect.class);
		assetManager.load("explosion-s.p", ParticleEffect.class);
		assetManager.load("beateffect.p", ParticleEffect.class);
	}

	@Override
	public void setupSkin(Skin skin) {
		skin.add("default", Color.WHITE);
		skin.add("inverse", Color.BLACK);
		
		TextButtonStyle defaultTextButton = new TextButtonStyle();
		defaultTextButton.up = skin.getDrawable("rect");
		defaultTextButton.down = skin.getDrawable("rect-down");
		defaultTextButton.font = skin.getFont("default-font");
		defaultTextButton.fontColor = skin.getColor("default");
		defaultTextButton.disabled = skin.getDrawable("rect-disabled");
		skin.add("default", defaultTextButton);
		
		TextButtonStyle subTextbutton = new TextButtonStyle(defaultTextButton);
		subTextbutton.font = skin.getFont("sub-font");
		skin.add("sub", subTextbutton);
		
		TextButtonStyle windowTextButton = new TextButtonStyle(defaultTextButton);
		windowTextButton.font = skin.getFont("window-font");
		skin.add("window", windowTextButton);
		
		SliderStyle defaultSlider = new SliderStyle(skin.getDrawable("default-slider"), skin.getDrawable("default-slider-knob"));
		skin.add("default-horizontal", defaultSlider);
		
		SliderStyle vertSlider = new SliderStyle(defaultSlider);
		vertSlider.knob = skin.getDrawable("vertical-slider-knob");
		skin.add("default-vertical", vertSlider);
		
		ProgressBarStyle defaultProgressBar = new ProgressBarStyle(skin.getDrawable("default-slider"), skin.getDrawable("progress-slider-knob"));
		
		LabelStyle defaultLabel = new LabelStyle();
		defaultLabel.font = skin.getFont("default-font");
		defaultLabel.fontColor = skin.getColor("default");
		skin.add("default", defaultLabel);
		
		TextFieldStyle defaultTextField = new TextFieldStyle(skin.getFont("sub-font"), skin.getColor("default"), skin.getDrawable("cursor"), skin.getDrawable("selection"), skin.getDrawable("textfield"));
		skin.add("default", defaultTextField);
		
		TextFieldStyle uiTextField = new TextFieldStyle(defaultTextField);
		uiTextField.font = skin.getFont("window-font");
		skin.add("ui", uiTextField);
		
		WindowStyle defaultWindow = new WindowStyle(skin.getFont("window-font"), skin.getColor("default"), skin.getDrawable("default-window"));
		skin.add("default", defaultWindow);
		
		WindowStyle tintedWindow = new WindowStyle(defaultWindow);
		tintedWindow.titleFontColor = skin.getColor("inverse");
		tintedWindow.background = skin.getDrawable("tinted-window");
		skin.add("tinted", tintedWindow);
		
		ListStyle defaultList = new ListStyle(skin.getFont("window-font"), skin.getColor("inverse"), skin.getColor("default"), skin.getDrawable("selection"));
		skin.add("default", defaultList);
		
		ScrollPaneStyle defaultScrollPane = new ScrollPaneStyle();
		defaultScrollPane.vScroll = skin.getDrawable("default-scroll");
		defaultScrollPane.hScrollKnob = skin.getDrawable("default-round-large");
		defaultScrollPane.hScroll = skin.getDrawable("default-scroll");
		defaultScrollPane.vScrollKnob = skin.getDrawable("default-round-large");
		skin.add("default", defaultScrollPane);
		
		CheckBoxStyle defaultCheckBox = new CheckBoxStyle(skin.getDrawable("check-off"), skin.getDrawable("check-on"), skin.getFont("window-font"), skin.getColor("default"));
		defaultCheckBox.checkboxOffDisabled = skin.getDrawable("check-disabled");
		skin.add("default", defaultCheckBox);
		
		SelectBoxStyle defaultSelectBox = new SelectBoxStyle(skin.getFont("default-font"), skin.getColor("default"), skin.getDrawable("default-select"), defaultScrollPane, defaultList);
		skin.add("default", defaultSelectBox);
		
		Gdx.app.debug("Prelaunch Debug Info", "UI Skin has been defined.");
		
		CheckBoxStyle playButtonStyle = new CheckBoxStyle(defaultCheckBox);
		playButtonStyle.checkboxOn = skin.getDrawable("play-down");
		playButtonStyle.checkboxOff = skin.getDrawable("play");
		skin.add("play-button", playButtonStyle);
		
		ImageButtonStyle pauseButtonStyle = new ImageButtonStyle();
		pauseButtonStyle.down = skin.getDrawable("pause-down");
		pauseButtonStyle.up = skin.getDrawable("pause");
		skin.add("pause-button", pauseButtonStyle);
		
		ImageButtonStyle fastForwardButtonStyle = new ImageButtonStyle();
		fastForwardButtonStyle.down = skin.getDrawable("fast-forward-down");
		fastForwardButtonStyle.up = skin.getDrawable("fast-forward");
		skin.add("fast-forward-button", fastForwardButtonStyle);
		
		ImageButtonStyle reverseButtonStyle = new ImageButtonStyle();
		reverseButtonStyle.down = skin.getDrawable("rewind-down");
		reverseButtonStyle.up = skin.getDrawable("rewind");
		skin.add("rewind-button", reverseButtonStyle);
		
		CheckBoxStyle shuffleButtonStyle = new CheckBoxStyle(defaultCheckBox);
		shuffleButtonStyle.checkboxOff = skin.getDrawable("shuffle");
		shuffleButtonStyle.checkboxOn = skin.getDrawable("shuffle-down");
		skin.add("shuffle-button", shuffleButtonStyle);
		
		ButtonStyle selectableButton = new ButtonStyle();
		selectableButton.checked = skin.getDrawable("holo-pane-down");
		selectableButton.up = skin.getDrawable("holo-pane");
		skin.add("selectable-button", selectableButton);
		
		ButtonStyle selectableCleanButton = new ButtonStyle();
		skin.add("selectable-clean-button", selectableCleanButton);
	}

	@Override
	public void generateFonts(Skin skin) {
		int height = Gdx.graphics.getHeight();
		
		skin.add("window-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
			{
				size = 18;
			}
		}));
		skin.add("sub-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
			{
				size = fontScale(0.05f, height);
			}
		}));
		skin.add("default-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
			{
				size = fontScale(0.07f, height);
			}
		}));
		skin.add("large-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
			{
				size = fontScale(0.085f, height);
			}
		}));
		skin.add("special-font", darktech_ldr_fontGenerator.generateFont(new FreeTypeFontParameter() {
			{
				size = fontScale(0.075f, height);
			}
		}));
	}
	
	public int fontScale(float fontSize, int height) {
		int size = MathUtils.round(Gdx.graphics.getDensity()*(fontSize*height));
		if (size >= 200) {
			size = 200;
		}
		return size;
	}
	
	@Override
	public void complete(AssetManager assetManager) {
		assetManager.get("standard_thrust.p", ParticleEffect.class).flipY();
		
		Preferences controlsPrefs = Gdx.app.getPreferences("RhythmBullet_Controls");
		Controls.UP.loadKeyFromPreferences(controlsPrefs);
		Controls.DOWN.loadKeyFromPreferences(controlsPrefs);
		Controls.LEFT.loadKeyFromPreferences(controlsPrefs);
		Controls.RIGHT.loadKeyFromPreferences(controlsPrefs);
		Controls.SHOOT.loadKeyFromPreferences(controlsPrefs);
		Controls.ACCELERATE.loadKeyFromPreferences(controlsPrefs);
	}
	
	@Override
	public void dispose() {
		darktech_ldr_fontGenerator.dispose();
		default_fontGenerator.dispose();
	}
}
