using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;

public class CharacterGenerator : MonoBehaviour {

    public GameObject character;
    List<Sprite> maleBases;
    List<Sprite> maleFaces;
    List<Sprite> maleHair;
    List<Sprite> maleHats;
    List<Sprite> maleShirts;
    List<Sprite> maleSuspenders;
    List<Sprite> femaleBases;
    List<Sprite> femaleFaces;
    List<Sprite> femaleHair;
    List<Sprite> femaleHats;
    List<Sprite> femaleShirts;

    Image characterBase;
    Image characterFace;
    Image characterHair;
    Image characterHat;
    Image characterShirt;
    Image characterSuspenders;

    Sprite edric;
    Sprite rijan;
    Sprite sennal;
    Sprite hornyBard;
    Sprite mug;
    Sprite rochelle;

    public List<string> allCharacters = new List<string>();
    void Start() {
        edric = Resources.Load<Sprite>("Sprites/special NPC convo sprites/edric");
        rijan = Resources.Load<Sprite>("Sprites/special NPC convo sprites/rijan");
        sennal = Resources.Load<Sprite>("Sprites/special NPC convo sprites/sennal");
        hornyBard = Resources.Load<Sprite>("Sprites/special NPC convo sprites/horny bard");
        mug = Resources.Load<Sprite>("Sprites/special NPC convo sprites/mug character");
        rochelle = Resources.Load<Sprite>("Sprites/special NPC convo sprites/rochelle");

        allCharacters = new List<string>() {
            "edric",
            "mug",
            "rijan",
            "sennal",
            "horny bard",
            "rochelle"
        };

        maleBases = (Resources.LoadAll<Sprite>("Sprites/male character assets/base")).ToList();
        maleFaces = (Resources.LoadAll<Sprite>("Sprites/male character assets/face")).ToList();
        maleHair = (Resources.LoadAll<Sprite>("Sprites/male character assets/hair")).ToList();
        //maleHats = (Resources.LoadAll<Sprite>("Sprites/male character assets/hat")).ToList();
        maleShirts = (Resources.LoadAll<Sprite>("Sprites/male character assets/shirt")).ToList();
        maleSuspenders = (Resources.LoadAll<Sprite>("Sprites/male character assets/suspenders")).ToList();

        femaleBases = (Resources.LoadAll<Sprite>("Sprites/female character assets/base")).ToList();
        femaleFaces = (Resources.LoadAll<Sprite>("Sprites/female character assets/face")).ToList();
        femaleHair = (Resources.LoadAll<Sprite>("Sprites/female character assets/hair")).ToList();
        //femaleHats = (Resources.LoadAll<Sprite>("Sprites/female character assets/hat")).ToList();
        femaleShirts = (Resources.LoadAll<Sprite>("Sprites/female character assets/shirt")).ToList();

        characterBase = character.transform.Find("Base").GetComponentInChildren<Image>();
        characterFace = character.transform.Find("Face").GetComponentInChildren<Image>();
        characterHair = character.transform.Find("Hair").GetComponentInChildren<Image>();
        //characterHat = character.transform.Find("Hat").GetComponentInChildren<Image>();
        characterShirt = character.transform.Find("Shirt").GetComponentInChildren<Image>();
        characterSuspenders = character.transform.Find("Suspenders").GetComponentInChildren<Image>();

        character.SetActive(false);
    }

    public void HideCharacter() {
        TogglePartsActive(false);
        character.SetActive(false);
    }

    public void GenerateRandomMaleCharacter() {
        TogglePartsActive(true);
        characterSuspenders.enabled = false;
        //characterHat.enabled = false;

        int rand = Random.Range(0, 15);
        //bool hasHat = (rand & (1 << 1)) != 0;
        bool hasSuspenders = (rand & (1 << 2)) != 0;

        rand = Random.Range(0, maleBases.Count);
        characterBase.sprite = maleBases[rand];

        rand = Random.Range(0, maleFaces.Count);
        characterFace.sprite = maleFaces[rand];

        rand = Random.Range(0, maleHair.Count);
        characterHair.sprite = maleHair[rand];

        // if (hasHat) {
        //     characterHat.enabled = true;
        //     rand = Random.Range(0, maleHats.Count);
        //     characterHat.sprite = maleHats[rand];
        // }

        rand = Random.Range(0, maleShirts.Count);
        characterShirt.sprite = maleShirts[rand];

        hasSuspenders = false;
        if (hasSuspenders) {
            characterSuspenders.enabled = true;
            rand = Random.Range(0, maleSuspenders.Count);
            characterSuspenders.sprite = maleSuspenders[rand];
        }

        character.SetActive(true);
    }

    public void GenerateRandomFemaleCharacter() {
        TogglePartsActive(true);
        characterSuspenders.enabled = false;
        //characterHat.enabled = false;

        int rand = Random.Range(0, 420);
        //bool hasHat = rand % 2 == 0;

        rand = Random.Range(0, femaleBases.Count);
        Debug.Log(femaleBases[rand].name);
        characterBase.sprite = femaleBases[rand];

        rand = Random.Range(0, femaleFaces.Count);
        characterFace.sprite = femaleFaces[rand];

        rand = Random.Range(0, femaleHair.Count);
        characterHair.sprite = femaleHair[rand];

        // if (hasHat) {
        //     characterHat.enabled = true;
        //     rand = Random.Range(0, femaleHats.Count);
        //     characterHat.sprite = femaleHats[rand];
        // }

        rand = Random.Range(0, femaleShirts.Count);
        characterShirt.sprite = femaleShirts[rand];

        character.SetActive(true);
    }

    public void GenerateRandomMixedCharacter() {
        TogglePartsActive(true);
        characterSuspenders.enabled = false;
        //characterHat.enabled = false;

        int rand = Random.Range(0, 15);
        //bool hasHat = (rand & (1 << 1)) != 0;
        bool hasSuspenders = (rand & (1 << 2)) != 0;

        List<Sprite> mixedBases = new List<Sprite>();
        mixedBases.AddRange(maleBases);
        mixedBases.AddRange(femaleBases);

        List<Sprite> mixedFaces = new List<Sprite>();
        mixedFaces.AddRange(maleFaces);
        mixedFaces.AddRange(femaleFaces);

        List<Sprite> mixedHair = new List<Sprite>();
        mixedHair.AddRange(maleHair);
        mixedHair.AddRange(femaleHair);

        List<Sprite> mixedShirts = new List<Sprite>();
        mixedShirts.AddRange(maleShirts);
        mixedShirts.AddRange(femaleShirts);

        rand = Random.Range(0, mixedBases.Count);
        Debug.Log(mixedBases[rand].name);
        characterBase.sprite = mixedBases[rand];

        rand = Random.Range(0, mixedFaces.Count);
        characterFace.sprite = mixedFaces[rand];

        rand = Random.Range(0, mixedHair.Count);
        characterHair.sprite = mixedHair[rand];

        // if (hasHat) {
        //     characterHat.enabled = true;
        //     rand = Random.Range(0, maleHats.Count);
        //     characterHat.sprite = maleHats[rand];
        // }

        rand = Random.Range(0, mixedShirts.Count);
        characterShirt.sprite = mixedShirts[rand];

        if (hasSuspenders) {
            characterSuspenders.enabled = true;
            rand = Random.Range(0, maleSuspenders.Count);
            characterSuspenders.sprite = maleSuspenders[rand];
        }

        character.SetActive(true);
    }

    public void GenerateNamedCharacter(string name) {
        TogglePartsActive(false);

        switch(name) {
            case "edric":
                characterBase.sprite = edric;
                break;
            case "rijan":
                characterBase.sprite = rijan;
                break;
            case "sennal":
                characterBase.sprite = sennal;
                break;
            case "horny bard":
                characterBase.sprite = hornyBard;
                break;
            case "mug":
                characterBase.sprite = mug;
                break;
            case "rochelle":
                characterBase.sprite = rochelle;
                break;
            default:
                break;
        }

        character.SetActive(true);
    }

    private void TogglePartsActive(bool isRandomNpc) {
        characterFace.enabled = isRandomNpc;
        characterHair.enabled = isRandomNpc;
        //characterHat.enabled = isRandomNpc;
        characterShirt.enabled = isRandomNpc;
        characterSuspenders.enabled = isRandomNpc;
    }

}
