using System.Windows.Forms;
using WebDumpLib.Core;

namespace WebDump
{
    public partial class WorkerStatus : UserControl
    {
        public int WorkerId { get; set; }
        public bool ParentInvalidator { get; set; }

        private string _WorkerName { get; set; }
        private string WorkerName
        {
            get { return _WorkerName; }
            set
            {
                if (_WorkerName == value) return;
                Invalidate();
                WorkerNameUpdated = true;
                _WorkerName = value;
            }
        }
        private bool WorkerNameUpdated { get; set; }

        private string _Board { get; set; }
        private string Board
        {
            get { return _Board; }
            set
            {
                if (value == _Board) return;
                Invalidate();
                BoardUpdated = true; _Board = value;
            }
        }
        private bool BoardUpdated { get; set; }

        private string _Task { get; set; }
        private string Task
        {
            get { return _Task; }
            set
            {
                if (value == _Task) return;
                Invalidate();
                TaskUpdated = true; _Task = value;
            }
        }
        private bool TaskUpdated { get; set; }

        private int _Progress { get; set; }
        private int Progress
        {
            get { return _Progress; }
            set
            {
                if (value == _Progress) return;
                Invalidate();
                ProgressUpdated = true;
                _Progress = value;
            }
        }
        private bool ProgressUpdated { get; set; }

        public WorkerStatus()
        {
            InitializeComponent();
            //DoubleBuffered = true;
            WorkerId = -1;
        }

        public void Update(BackgroundWorkerWrapper e)
        {
            if (e == null) return;
            WorkerName = e.Name;

            if (e.CurrentJobId == -1)
            {
                Board = "Worker";
                Task = "Idle";
                Progress = 0;
            }
            else
            {
                Task = e.ThreadName;
                Board = e.JobName;
                Progress = e.CurrentJobProgress?.OverallPercentage() ?? 0;
            }
        }

        public void Deassign()
        {
            WorkerId = -1;
            Invalidate();
            ParentInvalidator = true;
        }

        private void WorkerStatus_Paint(object sender, PaintEventArgs e)
        {
            if (WorkerId == -1)
            {
                Visible = false;
                return;
            }
            Visible = true;

            if (WorkerNameUpdated)
            {
                workerName.Text = WorkerName;
                WorkerNameUpdated = false;
            }

            if (BoardUpdated)
            {
                workerBoard.Text = Board;
                BoardUpdated = false;
            }

            if (TaskUpdated)
            {
                workerTask.Text = Task;
                TaskUpdated = false;
            }

            if (!ProgressUpdated) return;
            overallProgressBar.Value = Progress;
            ProgressUpdated = false;
        }
    }
}
