using Assets;
using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using static Questions;

public class PersonController : MonoBehaviour
{
    public string Gender { get; set; }
    public string Name { get; set; }
    public List<string> HairColors = new List<string> { "B", "Y", "O", "D" };

    [SerializeField] DatingController DatingController;

    [SerializeField] Image HairBack;
    [SerializeField] Image HairFront;
    [SerializeField] Image Head;
    [SerializeField] Image Eyes;
    [SerializeField] Image Nose;
    [SerializeField] Image Mouth;

    public List<Question> Questions;
    public List<Answer> Answers;

    void Start()
    {

    }

    void Update()
    {

    }

    public int EvaluateGift(Gift gift)
    {
        var total = 0;
        foreach(var answer in Answers)
        {
            total += gift.Aspects.Intersect(answer.Positives).Count();
            total -= gift.Aspects.Intersect(answer.Negatives).Count();
        }

        return total;
    }

    public void Randomize()
    {
        if (string.IsNullOrEmpty(DatingController.Preference) || DatingController.Preference == "Both")
        {
            var random = Random.Range(0, 2);
            Gender = random == 0 ? "M" : "F";
        }
        else Gender = DatingController.Preference;

        Name = Gender == "M" ? ArrayUtilities.GetRandomItem(Names.MaleNames) : ArrayUtilities.GetRandomItem(Names.FemaleNames);

        var hairColor = ArrayUtilities.GetRandomItem(HairColors);
        var head = Random.Range(1, 10);
        var eyes = Random.Range(1, 4);
        var mouth = Random.Range(1, 4);
        var nose = Random.Range(1, 4);
        var hairBack = Random.Range(1, 4);
        var hairFront = Random.Range(1, 4);

        if (Gender == "M")
        {
            HairBack.gameObject.SetActive(false);
        }
        else
        {
            HairBack.sprite = DatingController.FaceSprites[$"HairBack_{Gender}_{hairBack}_{hairColor}"];
            HairBack.gameObject.SetActive(true);
        }
        HairFront.sprite = DatingController.FaceSprites[$"HairFront_{Gender}_{hairFront}_{hairColor}"];
        Eyes.sprite = DatingController.FaceSprites[$"Eyes_{Gender}_{eyes}"];
        Mouth.sprite = DatingController.FaceSprites[$"Mouth_{Gender}_{mouth}"]; 
        Nose.sprite = DatingController.FaceSprites[$"Nose_{nose}"];
        Head.sprite = DatingController.FaceSprites[$"Head_{head}"];

        var questions = ArrayUtilities.GetRandomSubset(QuestionList, 3);
        Questions = new List<Question>();
        Answers = new List<Answer>();
        foreach(var question in questions)
        {
            Questions.Add(question);
            Answers.Add(ArrayUtilities.GetRandomItem(question.Answers));
        }
    }
}
