using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class PlayerConfiguration : MonoBehaviour {

    public string playerName = "Narune";
    public Color playerColor = Color.white;

    public void Awake() {
        DontDestroyOnLoad(this.gameObject);
        LoadConfigurationFromPrefs(); 
        OnLevelWasLoaded();
    }

    private void LoadConfigurationFromPrefs() {
        playerName = PlayerPrefs.GetString("Player.1.Name");
        playerColor = LoadColorFromPrefs("Player.1.Color");
    }

    private void SaveConfigurationToPrefs() {
        PlayerPrefs.SetString("Player.1.Name", playerName);
        SaveColorToPrefs("Player.1.Color", playerColor);
    }

    public void SetName(string name) {
        playerName = name;
        SaveConfigurationToPrefs();
    }

    public void SetColor(Color newPlayerColor) {
        this.playerColor = newPlayerColor;
        SaveConfigurationToPrefs();
    }

    public void OnLevelWasLoaded() {
        if (Application.loadedLevelName.Equals("AStartmenu")) {
            RegisterForNameChanges(); 
            RegisterForColorChanges();
        } else if (Application.loadedLevelName.StartsWith("Level")) {
            WriteColor();
        }
    }

    private void RegisterForNameChanges() {
        GameObject inputFieldPlayerNameObj
             = GameObject.Find("InputFieldPlayerName");

        if (inputFieldPlayerNameObj == null) {
            Debug.LogWarning(
                "Cannot find InputFieldPlayerName in scene!",
                this);
            return;
        }
        InputField inputFieldPlayerName
            = inputFieldPlayerNameObj.GetComponent<InputField>();

        inputFieldPlayerName.text = playerName;

        UnityAction<string> setName
            = new UnityAction<string>(SetName);

        inputFieldPlayerName.onValueChange.AddListener(setName);
    }

    private void RegisterForColorChanges() {
        GameObject colorPickerObj = GameObject.Find("ColorPicker");
        if (colorPickerObj == null) {
            Debug.LogWarning("Cannot find ColorPicker in scene!", this);
            return;
        }
        ColorPicker colorPicker
            = colorPickerObj.GetComponent<ColorPicker>();
        colorPicker.Color = playerColor;
        UnityAction<Color> setColor
            = new UnityAction<Color>(SetColor);
        colorPicker.onColorChanged.AddListener(setColor);
    }

    private void WriteColor() {
        GameObject tracerObj = GameObject.Find("Tracer-00");
        if (tracerObj == null) {
            Debug.LogWarning("Cannot find Tracer-00 in scene!", this);
            return;
        }
        TracerColor tracerColor
            = tracerObj.GetComponent<TracerColor>();
        tracerColor.playerColor = this.playerColor;
    }

    private Color LoadColorFromPrefs(string key) {
        float r = PlayerPrefs.GetFloat(PostfixKey(key, "R"));
        float g = PlayerPrefs.GetFloat(PostfixKey(key, "G"));
        float b = PlayerPrefs.GetFloat(PostfixKey(key, "B"));
        return new Color(r, g, b);
    }

    private void SaveColorToPrefs(string key, Color color) {
        PlayerPrefs.SetFloat(PostfixKey(key, "R"), color.r);
        PlayerPrefs.SetFloat(PostfixKey(key, "G"), color.g);
        PlayerPrefs.SetFloat(PostfixKey(key, "B"), color.b);
    }

    private string PostfixKey(string key, string postfix) {
        return string.Format("{0}.{1}", key, postfix);
    }

}
