using System;
using eagle;
using Net.Eagle0.Eagle.Api;
using Net.Eagle0.Eagle.Common;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class RunningGameItem : MonoBehaviour {
    public GameObject item;
    public TextMeshProUGUI gameIdField;
    public TextMeshProUGUI leaderField;
    public TextMeshProUGUI lastPlayedField;
    public RawImage professionImage;
    public Button dropButton;
    public GameObject tutorialIndicator;

    public Action<long> GoCallback;
    public Action<long> DropCallback;

    private long gameId;

    // Use this for initialization
    void Start() {}

    public void GoClicked() { GoCallback(gameId); }

    public void DropClicked() { DropCallback?.Invoke(gameId); }

    public void SetRunningGame(
            long gameId,
            AvailableLeader leader,
            long lastPlayedTimestampMillis,
            GameType gameType) {
        this.gameId = gameId;

        gameIdField.text = string.Format("{0:X}", gameId);
        // Use pre-resolved name from server if available, otherwise fall back to text lookup
        string leaderName;
        if (!string.IsNullOrEmpty(leader.Name)) {
            leaderName = leader.Name;
        } else {
            var textEntry = ClientTextProvider.Provider.GetTextEntry(leader.NameTextId);
            leaderName = textEntry != null ? textEntry.Text : "Hero";
        }
        leaderField.text = string.Format(
                "{0} ({1})",
                leaderName,
                DisplayNames.ProfessionNames[leader.Profession]);

        // Set profession image
        professionImage.texture = EagleCommonTextures.Instance.Profession(leader.Profession);
        professionImage.color = professionImage.texture != null ? Color.white : Color.clear;

        // Show/hide tutorial indicator
        if (tutorialIndicator != null) {
            tutorialIndicator.SetActive(gameType == GameType.Tutorial);
        }

        // Display last played time in user's local timezone
        if (lastPlayedField != null) {
            if (lastPlayedTimestampMillis > 0) {
                var lastPlayed = DateTimeOffset.FromUnixTimeMilliseconds(lastPlayedTimestampMillis)
                                         .LocalDateTime;
                var now = DateTime.Now;
                var diff = now - lastPlayed;

                string timeText;
                if (diff.TotalMinutes < 1) {
                    timeText = "Just now";
                } else if (diff.TotalHours < 1) {
                    timeText = $"{(int)diff.TotalMinutes}m ago";
                } else if (diff.TotalDays < 1) {
                    timeText = $"{(int)diff.TotalHours}h ago";
                } else if (diff.TotalDays < 7) {
                    timeText = $"{(int)diff.TotalDays}d ago";
                } else {
                    timeText = lastPlayed.ToString("MMM d");
                }
                lastPlayedField.text = timeText;
            } else {
                lastPlayedField.text = "";
            }
        }
    }
}
