using System;
using System.Collections.Generic;
using UnityEngine;
using mgaia.Utils;
using mgaia.Utils.Text;
using mgaia.tk2d;

public class OpenedLetter : MonoBehaviour
{
    [SerializeField]
    private tk2dBaseSprite _letterBackground;

    [SerializeField]
    private tk2dTextMesh _letterText;

    [SerializeField]
    private tk2dBaseSprite _stamp;

    [SerializeField]
    private tk2dTextMesh[] _choiceText;

    [SerializeField]
    private tk2dTextMesh _signature;

    [SerializeField]
    private GameObject _choicesContainer;

    [SerializeField]
    private GameObject _okButton;

    [SerializeField]
    private Transform _backgroundTransform;

    private float mYOriginalPosition;
    private int mNumberOfLinesInLetterText;
    private int mNumberOfLinesInChoiceText;
    private int mNumberOfLinesInSignatureText;
    private float mWidthOfLetterText;
    private float mWidthOfChoiceText;
    private float mWidthOfSignatureText;
    private Vector3 mScaleOfLetterText;
    private Vector3 mScaleOfChoiceText;
    private Vector3 mScaleOfSignatureText;

    private void Awake()
    {
        mYOriginalPosition = _backgroundTransform.localPosition.y;

        mNumberOfLinesInLetterText = GameConstants.NUMBER_OF_LINES_IN_LETTER;
        mNumberOfLinesInChoiceText = _choiceText.First().text.GetNumberOfLines();
        mNumberOfLinesInSignatureText = _signature.text.GetNumberOfLines();

        mWidthOfLetterText = _letterText.GetEstimatedMeshBoundsForString(_letterText.text).size.x;
        mWidthOfChoiceText = _choiceText.First().GetEstimatedMeshBoundsForString(_choiceText.First().text).size.x;
        mWidthOfSignatureText = _signature.GetEstimatedMeshBoundsForString(_signature.text).size.x;

        mScaleOfLetterText = _letterText.scale;
        mScaleOfChoiceText = _choiceText.First().scale;
        mScaleOfSignatureText = _signature.scale;
    }

    public void SetLetter(Letter letter, Choice[] choices)
    {
        _backgroundTransform.localPosition = new Vector3(_backgroundTransform.localPosition.x, (letter is RequestLetter) ? mYOriginalPosition : 0, _backgroundTransform.localPosition.z);

        _letterText.text = letter.TextBody;
        _letterText.scale = mScaleOfLetterText;
        TextMeshFormatter.FormatTextMesh(new Tk2dTextMeshContainer(_letterText), mNumberOfLinesInLetterText, mWidthOfLetterText, true);

        _signature.text = letter.Sender;
        _signature.scale = mScaleOfSignatureText;
        TextMeshFormatter.FormatTextMesh(new Tk2dTextMeshContainer(_signature), mNumberOfLinesInSignatureText, mWidthOfSignatureText, true);

        _choicesContainer.SetActive(letter is RequestLetter);
        _okButton.SetActive(letter is ResponseLetter);

        Attributes relatedAttribute = letter.RelatedAttribute;
        Stamps stampToSet = relatedAttribute.GetStamp();
        LetterBackgrounds letterBackgroundToSet = relatedAttribute.GetLetterBackground();

        _stamp.spriteId = (int)stampToSet;
        _letterBackground.spriteId = (int)letterBackgroundToSet;

        if (choices != null && choices.Length > 0)
        {
            Assert.AssertTrue(letter is RequestLetter, "Quando há escolhas a letter tem que ser do tipo RequestLetter!");

            for (int idx = 0; idx < _choiceText.Length; idx++)
            {
                _choiceText[idx].text = choices[idx].TextBody;
                _choiceText[idx].scale = mScaleOfChoiceText;
                TextMeshFormatter.FormatTextMesh(new Tk2dTextMeshContainer(_choiceText[idx]), mNumberOfLinesInChoiceText, mWidthOfChoiceText, true);
            }
        }
    }
}