using System.Media;

namespace WinGreed
{
    public partial class frmStart : Form
    {
        private bool GameOver = false;
        private GameOverCheck gocFirst = new GameOverCheck();
        private GameOverCheck gocSecond = new GameOverCheck();
        private frmPersonRound fPR = null;
        private int actPlayerNo = -1;
        public frmStart()
        {
            InitializeComponent();
        }

        private void btnSpela_Click(object sender, EventArgs e)
        {
            if (lvMemberList.Items.Count > 0)
            {
                var plNr = NextLvItem();
                var nextLvInstance = lvMemberList.Items[plNr];
                var nextPlayerName = nextLvInstance.Text;

                if (!GameOver || (GameOver && nextLvInstance != gocFirst.lvsave))
                {
                    fPR = new frmPersonRound(nextPlayerName);
                    fPR.TotPoints = int.Parse(nextLvInstance.SubItems[2].Text);
                    fPR.ShowDialog();
                    nextLvInstance.SubItems[2].Text = fPR.TotPoints.ToString();
                    if (fPR.TotPoints > 0 && fPR.AddedPoints>0)
                    {
                        lstLogBox.Items.Add($"{nextPlayerName} -> {fPR.AddedPoints} poäng.");
                    }
                }

                if (GameOver)
                {
                    if (nextLvInstance == gocFirst.lvsave)
                    {
                        PlaySomething();
                        if (gocSecond.lvsave != null)
                        {
                            gocSecond.lvsave.SubItems[1].Text = (int.Parse(gocSecond.lvsave.SubItems[1].Text) + 1).ToString();
                        }
                        else
                        {
                            nextLvInstance.SubItems[1].Text = (int.Parse(nextLvInstance.SubItems[1].Text) + 1).ToString();
                        }
                        btnSpela.Enabled = false;
                    }
                    else
                    {
                        if(fPR.TotPoints > gocFirst.points && nextLvInstance != gocFirst.lvsave)
                        {
                            if(nextLvInstance != gocSecond.lvsave && fPR.TotPoints > gocSecond.points)
                            {
                                gocSecond.points = fPR.TotPoints;
                                gocSecond.lvsave = nextLvInstance;
                            }
                        }
                    }
                }
                else
                {
                    if (fPR.TotPoints > 10000)
                    {
                        GameOver = true;
                        gocFirst.points = fPR.TotPoints;
                        gocFirst.lvsave = nextLvInstance;
                    }
                }
                plNr = actPlayerNo;
                btnSpela.Text = $"Låt {lvMemberList.Items[NextLvItem()].Text} spela";
                actPlayerNo = plNr;
            }
        }

        private int NextLvItem()
        {
            var retValue = 0;
            if (lvMemberList.Items.Count > 1)
            {
                actPlayerNo++;
                if (actPlayerNo > lvMemberList.Items.Count - 1)
                {
                    actPlayerNo = 0;
                }
                retValue = actPlayerNo;
            }
            return retValue;
        }

        private void btnAddPlayer_Click(object sender, EventArgs e)
        {
            txtNewName.Visible = true;

        }

        private void btnAddOk_Click(object sender, EventArgs e)
        {
            var item = lvMemberList.Items.Add(txtNewName.Text.Trim());
            item.SubItems.Add("0");
            item.SubItems.Add("0");
            txtNewName.Text = "";
            txtNewName.Visible = false;
            btnAddOk.Visible = false;
            if (lvMemberList.Items.Count > 1)
            {
                btnSpela.Enabled = true;
                btnSpela.Text = $"Låt {lvMemberList.Items[0].Text} spela";
            }
        }

        private void txtNewName_TextChanged(object sender, EventArgs e)
        {
            if (txtNewName.Text.Trim().Length > 0)
            {
                btnAddOk.Visible = true;
            }
            else
            {
                btnAddOk.Visible = false;
            }
        }

        private void PlaySomething()
        {
            var soundPlayer = new SoundPlayer();
            soundPlayer.SoundLocation = @".\HUMAN_CROWD_Applause.wav";
            soundPlayer.Play();
        }

        private void btnSound_Click(object sender, EventArgs e)
        {
            PlaySomething();
        }
    }
}