using DarkTonic.CoreGameKit;
using UnityEngine;

[System.Serializable]
public class Eye
{
    public Transform eyePivot;
    public SpriteRenderer eyeOpen;
    public SpriteRenderer eyeClose;
    public SpriteRenderer eyebrow;
}

[System.Serializable]
public class Ear
{
    public SpriteRenderer ear;
    public SpriteRenderer earEarring;
}

[System.Serializable]
public class hasComponent
{
    public bool hasHair;
    public bool hasMustache;
    public bool hasBeard;
    public bool hasGoatee;
    public bool hasLeftEarring;
    public bool hasRightEarring;
    public bool noEarring;
}

public class Face : MonoBehaviour
{
    #region Face Components

    public SpriteRenderer hair;
    public SpriteRenderer head;

    public Ear earLeft;
    public Ear earRight;

    public Eye eyeLeft;
    public Eye eyeRight;

    [HideInInspector]
    public hasComponent component;

    public SpriteRenderer nose;
    public SpriteRenderer mustache;
    public SpriteRenderer mouth;
    public SpriteRenderer beard;

    public Transform chinPivot;

    public Transform[] chinList;

    #endregion

    #region Sprites

    public Sprite[] hairSprites;
    public Sprite[] headSprites;

    public Sprite[] earSprites;
    public Sprite[] earEarringSprites;

    public Sprite[] eyeOpenSprites;
    public Sprite[] eyeCloseSprites;
    public Sprite[] eyebrowSprites;

    public Sprite[] noseSprites;
    public Sprite[] mustacheSprites;
    public Sprite[] mouthSprites;
    public Sprite[] beardSprites;

    #endregion

    #region Colors

    public Color[] hairColors;
    public Color[] facialHairColors;
    public Color[] earEarringColors;
    public Color[] skinColors;

    #endregion

    private Chin currentChin;
    
    //private void OnDisable()
    //{
    //    if (this.currentChin != null)
    //    {
    //        PoolBoss.Despawn(this.currentChin.transform);
    //        this.currentChin = null;
    //    }
    //}

    public void makeRandomHead()
    {
        Debug.Log("Criando face");

        Color hairColor = this.hairColors[Random.Range(0, this.hairColors.Length)];
        Color facialHairColor = this.facialHairColors[Random.Range(0, this.facialHairColors.Length)];
        Color skinColor = this.skinColors[Random.Range(0, this.skinColors.Length)];
        Color earringColor = this.earEarringColors[Random.Range(0, this.earEarringColors.Length)];

        this.mustache.color = facialHairColor;
        this.beard.color = facialHairColor;

        this.head.sprite = this.sort(this.headSprites);
        this.head.color = skinColor;

        this.nose.sprite = this.sort(this.noseSprites);
        this.nose.color = skinColor;

        this.mustache.color = facialHairColor;

        this.mouth.sprite = this.sort(this.mouthSprites);
        this.mouth.color = skinColor;

        this.beard.color = facialHairColor;

        #region orelha aleatoria
        Sprite s = this.sort(this.earSprites);
        this.earLeft.ear.sprite = s;
        this.earLeft.ear.color = skinColor;
        this.earRight.ear.sprite = s;
        this.earRight.ear.color = skinColor;

        int sortNumber = Random.Range(1, 100);
        if(sortNumber % 2 == 0)
        {
            this.mustache.enabled = true;
            this.component.hasMustache = true;
            this.mustache.sprite = this.sort(this.mustacheSprites);
        }
        else
        {
            this.mustache.enabled = false;
            this.component.hasMustache = false;
        }
        #endregion

        #region barba ou cavanhaque aleatorios
        sortNumber = Random.Range(1, 10);
        if(sortNumber % 2 == 0)
        {
            Debug.Log("tem barba");
            this.setChin(facialHairColor, skinColor, false);
            this.beard.enabled = true;
            this.component.hasBeard = true;
            this.beard.sprite = this.sort(this.beardSprites);
        }
        else
        {
            Debug.Log("nao tem barba");
            this.beard.enabled = false;
            this.component.hasBeard = false;

            sortNumber = Random.Range(1, 10);
            if(sortNumber % 2  == 0)
            {
                Debug.Log("tem cavanhaque");
                this.setChin(facialHairColor, skinColor, true);
                this.component.hasGoatee = true;
            }
            else
            {
                Debug.Log("nao tem cavanhaque");
                this.setChin(facialHairColor, skinColor, false);
                this.component.hasGoatee = false;
            }
        }
        #endregion

        #region cabelo aleatorio
       
        this.hair.enabled = true;
        this.component.hasHair = true;
        this.hair.sprite = this.sortHair(this.hairSprites);

        if ((this.component.hasBeard || this.component.hasGoatee))
        {
            sortNumber = Random.Range(0, 50);
            if (sortNumber % 2 == 0)
            {
                if(this.component.hasBeard)
                {
                    Debug.Log("cabelo cor da barba" + sortNumber);
                    this.hair.color = this.beard.color;
                }
                else if(this.component.hasGoatee)
                {
                    Debug.Log("cabelo cor do cavanhaque" + sortNumber);
                    this.hair.color = this.currentChin.goatee.color;
                }       
            }
            else
            {
                Debug.Log("cabelo cores aleatorias" + sortNumber);
                this.hair.color = hairColor;
            }
        }
        else
        {
            this.hair.color = hairColor;
            Debug.Log("cabelo cores aleatorias sem barba ou goatee");
        }           
        
        #endregion 

        #region brinco aleatorio
        sortNumber = Random.Range(1, 30);
        s = this.sort(this.earEarringSprites);

        if ((sortNumber > 0 && sortNumber < 6) || (sortNumber > 20 && sortNumber < 26))
        {
            //brinco na esquerda
            this.component.hasLeftEarring = true;
            this.component.hasRightEarring = false;

            this.earLeft.earEarring.enabled = true;
            this.earRight.earEarring.enabled = false;

            this.earLeft.earEarring.sprite = s;
            this.earLeft.earEarring.color = earringColor;
        }
        else if ((sortNumber > 5 && sortNumber < 11) || (sortNumber > 25 && sortNumber < 31))
        {
            //brinco na direita
            this.component.hasLeftEarring = false;
            this.component.hasRightEarring = true;

            this.earLeft.earEarring.enabled = false;
            this.earRight.earEarring.enabled = true;

            this.earRight.earEarring.sprite = s;
            this.earRight.earEarring.color = earringColor;
        }
        else if((sortNumber > 10 && sortNumber < 16) || (sortNumber > 30 && sortNumber < 36))
        {
            //brinco em ambas as orelhas
            this.component.hasLeftEarring = true;
            this.component.hasRightEarring = true;

            this.earLeft.earEarring.enabled = true;
            this.earRight.earEarring.enabled = true;

            this.earLeft.earEarring.sprite = s;
            this.earRight.earEarring.sprite = s;

            this.earLeft.earEarring.color = earringColor;
            this.earRight.earEarring.color = earringColor;

        }
        else if ((sortNumber > 15 && sortNumber < 21) || (sortNumber > 35 && sortNumber < 41))
        {
            //sem brinco
            this.component.hasLeftEarring = false;
            this.component.hasRightEarring = false;

            this.earRight.earEarring.enabled = false;
            this.earLeft.earEarring.enabled = false;
        }
        #endregion

        #region sobrancelha aleatoria
        s = this.sort(this.eyebrowSprites);
        this.eyeLeft.eyebrow.sprite = s;
        this.eyeLeft.eyebrow.color = facialHairColor;
        this.eyeRight.eyebrow.sprite = s;
        this.eyeRight.eyebrow.color = facialHairColor;
        #endregion

        //olhos aleatorios
        this.setEye();        
    }

    public void makeOtherHeads(Face original)
    {
        Debug.Log("Criando face");
        int count = 0;
        Color hairColor = this.hairColors[Random.Range(0, this.hairColors.Length)];
        Color facialHairColor = this.facialHairColors[Random.Range(0, this.facialHairColors.Length)];
        Color skinColor = this.skinColors[Random.Range(0, this.skinColors.Length)];
        Color earringColor = this.earEarringColors[Random.Range(0, this.earEarringColors.Length)];

        int sortNumber;

        this.mustache.color = facialHairColor;
        this.beard.color = facialHairColor;

        this.head.sprite = this.sort(this.headSprites);
        this.head.color = skinColor;

        this.nose.sprite = this.sort(this.noseSprites);
        this.nose.color = skinColor;

        this.mustache.color = facialHairColor;

        this.mouth.sprite = this.sort(this.mouthSprites);
        this.mouth.color = skinColor;

        this.beard.color = facialHairColor;

        Sprite s = this.sort(this.earSprites);
        this.earLeft.ear.sprite = s;
        this.earLeft.ear.color = skinColor;
        this.earRight.ear.sprite = s;
        this.earRight.ear.color = skinColor;

        if (original.component.hasMustache)
        {
            sortNumber = Random.Range(0, this.mustacheSprites.Length);
            this.mustache.enabled = true;
            this.component.hasMustache = true;
            Debug.Log("Com bigode");
            if (sortNumber % 2 == 0 && count < 2)
            {
                count++;
                Debug.Log("Com bigode original" + sortNumber);
                this.mustache.sprite = original.mustache.sprite;
            }
            else
            {
                Debug.Log("Com bigode aleatorio" + sortNumber);
                this.mustache.sprite = this.sort(this.mustacheSprites);
            }
        }
        else
        {
            Debug.Log("sem bigode");
            this.mustache.enabled = false;
            this.component.hasMustache = false;
        }

        if (original.component.hasBeard)
        {          
            sortNumber = Random.Range(0, this.beardSprites.Length);
            this.beard.enabled = true;
            this.component.hasBeard = true;

            this.setChin(facialHairColor, skinColor, false);
            this.currentChin.enabled = true;
            

            Debug.Log("tem barba");
            if (sortNumber % 2 == 0 && count < 2)
            {
                count++;
                Debug.Log("tem barba igual original" + sortNumber);
                this.beard.sprite = original.beard.sprite;
            }
            else
            {
                Debug.Log("tem barba aleatoria" + sortNumber);
                this.beard.sprite = this.sort(this.beardSprites);
            }
        }
        else
        {
            Debug.Log("nao tem barba");

            this.beard.enabled = false;
            this.component.hasBeard = false;
       
            if (original.component.hasGoatee)
            {
                Debug.Log("tem cavanhaque");

                this.component.hasGoatee = true;
                this.setChin(facialHairColor, skinColor, true);
                this.currentChin.enabled = true;
                
                sortNumber = Random.Range(0, this.currentChin.goateeSprites.Length);
                if(sortNumber % 2 == 0 && count < 2)
                {
                    count++;
                    Debug.Log("tem cavanhaque igual original" + sortNumber);
                    this.currentChin.goatee.sprite = original.currentChin.goatee.sprite;
                }
                else
                {
                    Debug.Log("tem cavanhaque aleatorio" + sortNumber);
                    this.currentChin.goatee.sprite = this.sort(this.currentChin.goateeSprites);
                }
            }
            else
            {
                Debug.Log("nao tem cavanhaque");
                this.setChin(facialHairColor, skinColor, false);
                this.component.hasGoatee = false;
            }
        }
        
        if(original.component.hasHair)
        {
            Debug.Log("tem cabelo");
            this.hair.enabled = true;
            this.component.hasHair = true;
            sortNumber = Random.Range(0, this.hairSprites.Length);
            if(sortNumber % 2 == 0 && count < 2)
            {
                count++;
                Debug.Log("tem cabelo igual original" + sortNumber);
                this.hair.sprite = original.hair.sprite;
            }
            else
            {
                Debug.Log("tem cabelo aleatorio" + sortNumber);
                this.hair.sprite = this.sortHair(this.hairSprites);
            }
        }
        else
        {
            Debug.Log("nao tem cabelo");
            this.hair.enabled = false;
            this.component.hasHair = false;
        }
        
        if ((this.component.hasBeard || this.component.hasGoatee))
        {

            if (this.component.hasBeard)
            {
                Debug.Log("cabelo cor da barba" );
                this.hair.color = this.beard.color;
            }
            else if (this.component.hasGoatee)
            {
                Debug.Log("cabelo cor do cavanhaque" );
                this.hair.color = this.currentChin.goatee.color;
            }
            else
            {
                Debug.Log("cabelo cores aleatorias");
                this.hair.color = hairColor;
            }
        }
        else
        {
            this.hair.color = hairColor;
            Debug.Log("cabelo cores aleatorias sem barba ou goatee");
        }

        s = this.sort(this.earEarringSprites);

        if ((original.component.hasLeftEarring))
        {
            //brinco na esquerda
            Debug.Log("brinco na esquerda");

            this.component.hasLeftEarring = true;
            this.component.hasRightEarring = false;

            this.earLeft.earEarring.enabled = true;
            this.earRight.earEarring.enabled = false;

            this.earLeft.earEarring.sprite = s;
            this.earLeft.earEarring.color = earringColor;
        }
        else if (original.component.hasRightEarring)
        {
            //brinco na direita
            Debug.Log("brinco na direita");
            this.component.hasLeftEarring = false;
            this.component.hasRightEarring = true;

            this.earLeft.earEarring.enabled = false;
            this.earRight.earEarring.enabled = true;

            this.earRight.earEarring.sprite = s;
            this.earRight.earEarring.color = earringColor;
        }
        else if (original.component.hasLeftEarring && original.component.hasRightEarring)
        {
            //brinco em ambas as orelhas
            Debug.Log("brinco em ambas");
            this.component.hasLeftEarring = true;
            this.component.hasRightEarring = true;

            this.earLeft.earEarring.enabled = true;
            this.earRight.earEarring.enabled = true;

            this.earLeft.earEarring.sprite = s;
            this.earRight.earEarring.sprite = s;

            this.earLeft.earEarring.color = earringColor;
            this.earRight.earEarring.color = earringColor;

        }
        else if (!original.component.hasLeftEarring && !original.component.hasRightEarring)
        {
            //sem brinco
            Debug.Log("sem brincos");
            this.component.hasLeftEarring = false;
            this.component.hasRightEarring = false;

            this.earRight.earEarring.enabled = false;
            this.earLeft.earEarring.enabled = false;
        }

        #region sobrancelha aleatoria
        s = this.sort(this.eyebrowSprites);
        this.eyeLeft.eyebrow.sprite = s;
        this.eyeLeft.eyebrow.color = facialHairColor;
        this.eyeRight.eyebrow.sprite = s;
        this.eyeRight.eyebrow.color = facialHairColor;
        #endregion

        //olhos aleatorios
        this.setEye();

        Debug.Log("\n");
    }
    
    private Sprite sort(Sprite[] spriteList)
    {
        Sprite result = null;

        if (spriteList != null && spriteList.Length > 0)
        {
            result = spriteList[Random.Range(0, spriteList.Length)];
        }

        return result;
    }

    private Sprite sortHair(Sprite[] spriteHairList)
    {
        int number = 0;
        Sprite result = null;
        if(spriteHairList != null && spriteHairList.Length > 0)
        {
            number = Random.Range(0, spriteHairList.Length);
            if(number > -1 && number < 2)
            {
                number = Random.Range(0, 1);
                result = spriteHairList[number];
            }
            else if(number > 1 && number < 4)
            {
                number = Random.Range(2, 3);
                result = spriteHairList[number];
            }
            else if(number > 3 && number < 9)
            {
                number = Random.Range(4, 8);
                result = spriteHairList[number];
            }
            else if (number > 8 && number < 13)
            {
                number = Random.Range(9, 12);
                if(number == 12)
                {
                    Debug.Log("careca");
                    this.hair.enabled = false;
                    this.component.hasHair = false;
                    return null;
                }
                else
                {
                    result = spriteHairList[number];
                }      
            }
        }
        this.hair.enabled = true;
        this.component.hasHair = true;
        Debug.Log("hair Number" + number);
        return result;
    }

    private void setEye()
    {
        if (this.eyeOpenSprites != null && this.eyeOpenSprites.Length > 0)
        {
           int index = Random.Range(0, this.eyeOpenSprites.Length);

           this.eyeLeft.eyeOpen.sprite = this.eyeOpenSprites[index];
           this.eyeLeft.eyeClose.sprite = this.eyeCloseSprites[index];
            
           this.eyeRight.eyeOpen.sprite = this.eyeOpenSprites[index];
           this.eyeRight.eyeClose.sprite = this.eyeCloseSprites[index];       
        }                       
    }

    private void setChin(Color facialHairColor, Color skinColor, bool enabled)
    {
        Transform chinPrefab = null;

        if (this.chinList != null && this.chinList.Length > 0)
        {
            chinPrefab = this.chinList[Random.Range(0, this.chinList.Length)];

            Transform trans = PoolBoss.Spawn(chinPrefab, this.chinPivot.position, Quaternion.identity, this.chinPivot);

            if (trans != null)
            {
                this.currentChin = trans.GetComponent<Chin>();
                this.currentChin.chin.color = skinColor;
                this.currentChin.setGoatee(facialHairColor, enabled);
            }
        }
    }
}