﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DotDotBot.TwitchApi;
using DotDotBot.Contracts;
using DotDotBot.Services;
using System.Timers;
using System.Windows.Threading;

namespace DotDotBot.GUI
{
    /// <summary>
    /// Interaktionslogik für pStatus.xaml
    /// </summary>
    public partial class pStatus : Page
    {

        string _channelName;


        public pStatus(Frame frame, ILogger log, string channelName)
        {
            InitializeComponent();
            _channelName = channelName;

            displayInfo(_channelName);


            var timer = new Timer
            {
                Interval = 1000,
                AutoReset = true
            };

            timer.Elapsed += OnTimerElapsed;
            timer.Enabled = true;
        }

        public void OnTimerElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {

            Dispatcher.Invoke(new Action(()=>{ displayInfo(_channelName); }));
        }


        public void displayInfo(string channelName)
        {

            lChannelName.Content = TwitchApiClient.GetTitle(channelName);
            lGameName.Content = TwitchApiClient.GetGameName(channelName);
            lViewerCount.Content = TwitchApiClient.GetNumberOfViewers(channelName);
            lFollowerCount.Content = TwitchApiClient.GetFollowerCount(channelName);
            if (TwitchApiClient.GetUptime(channelName).ToString().Length > 8)
            {
                lUptime.Content = TwitchApiClient.GetUptime(channelName).ToString().Remove(8);
            }
            else
            {
                lUptime.Content = TwitchApiClient.GetUptime(channelName).ToString();
            }

        }

    }
}
