using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;

public class RacerPreferences : MonoBehaviour {

	public Mesh[] hairModels;
	public Texture[] playerTextures, kartTextures;
	public MeshFilter hairStyle;
	public SkinnedMeshRenderer playerCharacter, playerKart;
	public MeshRenderer playerHair;

	public Transform kartRotator;

	public int activeHair, activePlayerText, activeKartText;

	public Slider hrs, krs, hgs, kgs, hbs, kbs, rotationSlider;

	public float hr, kr, hg, kg, hb, kb;

	public Color activeHairColor, activeKartColor;

    // Start is called before the first frame update
    void Start () {
		hairModels = Resources.LoadAll<Mesh> ("Hair Models");
		playerTextures = Resources.LoadAll<Texture2D> ("Player Textures");
		kartTextures = Resources.LoadAll<Texture2D> ("Kart Textures");

		if (CheckCharacterSave ())
        {

			hairStyle.mesh = (Mesh)Resources.Load ("Hair Models/" + PlayerPrefs.GetString ("HairStyle"));

            hr = PlayerPrefs.GetFloat ("HairColorR");
			hg = PlayerPrefs.GetFloat ("HairColorG");
			hb = PlayerPrefs.GetFloat ("HairColorB");

			playerCharacter.material.mainTexture = (Texture2D)Resources.Load ("Player Textures/" + PlayerPrefs.GetString ("PlayerTexture"));
			playerKart.material.mainTexture = (Texture2D)Resources.Load ("Kart Textures/" + PlayerPrefs.GetString ("KartTexture"));

            PlayerPrefs.GetString ("KartTexture");

            kr = PlayerPrefs.GetFloat ("KartColorR");
			kg = PlayerPrefs.GetFloat ("KartColorG");
			kb = PlayerPrefs.GetFloat ("KartColorB");


			if (playerHair) {
				playerHair.material.color = new Color (hr, hg, hb);
			}

			if (playerKart) {
				playerKart.materials[0].color = new Color (kr, kg, kb);
				playerKart.materials[3].color = new Color (kr, kg, kb);
			}

		}

		for (int p = 0; p < playerTextures.Length; p++) {
			if (playerCharacter.sharedMaterial.mainTexture == playerTextures [p]) {
				activePlayerText = p;
			}
		}

		for (int k = 0; k < kartTextures.Length; k++) {
			if (playerKart.sharedMaterial.mainTexture == kartTextures [k]) {
				activeKartText = k;
			}
		}

		for (int h = 0; h < hairModels.Length; h++) {
			if (hairStyle.sharedMesh == hairModels [h]) {
				activeHair = h;
			}
		}

		hrs.value = playerHair.material.color.r;
		hgs.value = playerHair.material.color.g;
		hbs.value = playerHair.material.color.b;

		krs.value = playerKart.material.color.r;
		kgs.value = playerKart.material.color.g;
		kbs.value = playerKart.material.color.b;

		rotationSlider.value = 180f;

    }

    // Update is called once per frame
    void Update () {
		playerCharacter.material.mainTexture = playerTextures [activePlayerText];
		playerKart.material.mainTexture = kartTextures [activeKartText];
		hairStyle.mesh = hairModels [activeHair];

		hr = hrs.value;
		hg = hgs.value;
		hb = hbs.value;

		kr = krs.value;
		kg = kgs.value;
		kb = kbs.value;

		kartRotator.transform.localEulerAngles = new Vector3 (kartRotator.transform.localEulerAngles.x, rotationSlider.value, kartRotator.transform.localEulerAngles.z);

		playerHair.material.color = new Color (hr, hg, hb);
		playerKart.materials[0].color = new Color (kr, kg, kb);
		playerKart.materials[3].color = new Color (kr, kg, kb);
    }

	public void Cycle (int LR) {
		if (LR == 0) {
			activePlayerText--;
			if (activePlayerText < 0) {
				activePlayerText = playerTextures.Length - 1;
			}
		} else if (LR == 1) {
			activePlayerText++;
			if (activePlayerText >= playerTextures.Length) {
				activePlayerText = 0;
			}
		} else if (LR == 2) {
			activeHair--;
			if (activeHair < 0) {
				activeHair = hairModels.Length - 1;
			}
		} else if (LR == 3) {
			activeHair++;
			if (activeHair >= hairModels.Length) {
				activeHair = 0;
			}
		} else if (LR == 4) {
			activeKartText--;
			if (activeKartText < 0) {
				activeKartText = kartTextures.Length - 1;
			}
		} else if (LR == 5) {
			activeKartText++;
			if (activeKartText >= kartTextures.Length) {
				activeKartText = 0;
			}
		}
	}

	public bool CheckCharacterSave () {

		string[] myKeys = new string[] {
			"HairStyle",
			"HairColorR",
			"HairColorG",
			"HairColorB",
			"PlayerTexture",
			"KartTexture",
			"KartColorR",
			"KartColorG",
			"KartColorB"
		};

		for (int i = 0; i < myKeys.Length; i++) {
			if (!PlayerPrefs.HasKey (myKeys [i])) {
				return false;
			}
		}
		return true;
	}

	public void SaveDefaultCharacter () {
		string playerHairStyle = hairStyle.sharedMesh.name;
		string playerText = playerCharacter.sharedMaterial.mainTexture.name;
		string kartText = playerKart.sharedMaterial.mainTexture.name;

		PlayerPrefs.SetString ("HairStyle", playerHairStyle);
		PlayerPrefs.SetFloat ("HairColorR", hr);
		PlayerPrefs.SetFloat ("HairColorG", hg);
		PlayerPrefs.SetFloat ("HairColorB", hb);


		PlayerPrefs.SetString ("PlayerTexture", playerText);
		PlayerPrefs.SetString ("KartTexture", kartText);
		PlayerPrefs.SetFloat ("KartColorR", kr);
		PlayerPrefs.SetFloat ("KartColorG", kg);
		PlayerPrefs.SetFloat ("KartColorB", kb);
	}
}
