using System;
using System.Collections.Generic;
using DxLibDLL;
using System.Collections;



namespace HH
{
    static class Program
    {
        public static Game game = new Game();
        /// <summary>
        /// Start
        /// </summary>
        [STAThread]
        static void Main()
        {
            DX.ChangeWindowMode(DX.TRUE);
            //DX.ChangeWindowMode(DX.FALSE);

            if (DX.DxLib_Init() == -1)		// DX start (init)
            {
                return;			// error -> quit
            }

            //default size = 640x480

            DX.SetDrawScreen(DX.DX_SCREEN_BACK);

            
            

            Player p1 = new Player();
            game.playerList.Add(p1);
            game.p1 = p1;


            int enemyComingCount = 0;
            string topleft = "";


            // game loop
            while (DX.ProcessMessage() == 0 && DX.CheckHitKey(DX.KEY_INPUT_ESCAPE) == 0)
            {
                game.update();

                ///////////////Updating data////////////////////

                // Update data of player and player's bullets
                
                foreach (CBasic pc in game.playerList)
                {
                    pc.update();
                }
                
                //Adding player's bullets
                game.playerList.AddRange(game.newPlayerList);
                game.newPlayerList.Clear();
                


                // Update data of enemies

                //Add an enemy // TMP RAND ver. /////////////////////////////////////
                enemyComingCount++;
                //if (enemyComingCount == 300)
                if (enemyComingCount == 200) 
                {
                    enemyComingCount = 0;

                    if (DX.GetRand(1) == 1)
                    {
                        EnemyCGGS e = new EnemyCGGS();
                        e.position.x = DX.GetRand(640);
                        e.velocity.x = (DX.GetRand(10) - 5) / 8;
                        game.enemyList.Add(e);
                    }
                    else
                    {
                      EnemySpiralDiverder e = new EnemySpiralDiverder();
                      e.position.x = DX.GetRand(640);
                      e.velocity.x = (DX.GetRand(10) - 5) / 8;
                      game.enemyList.Add(e);                        
                    }
                }
                


                if (game.table[game.timer] != null)
                {
                    game.enemyList.Add(game.table[game.timer]);
                }



                //updating
                foreach (CBasic en in game.enemyList)
                {
                    en.update();
                }
                
                //Adding enemies' bullets
                game.enemyList.AddRange(game.newEnemyList);
                game.newEnemyList.Clear();





                //////////Collision //////////////////////////////////

                

                //Player
                foreach (CBasic en in game.enemyList)
                {
                    if (isCollision(p1, en) == true)
                    {
                        //return;
                    }
 
                }

                foreach (CBasic en in game.enemyList)
                {
                    foreach (CBasic pl in game.playerList)
                    {
                        if (pl.kind == CBasic.CType.PlayerBullet
                            && en.kind == CBasic.CType.EnemyBullet)
                        { }
                        else
                        {
                            if (isCollision(pl, en) == true)
                            {
                                pl.life --;
                                en.life --;
                            }
                            if (pl.life == 0) { pl.isAlive = false; }
                            if (en.life == 0) { en.isAlive = false; }
                        }
                    }

                }

                

                //Removing player's characters

                for (int i = game.playerList.Count - 1; i >= 0; i--)
                {
                    if (((CBasic)game.playerList[i]).isAlive == false)
                    {
                        //if (((CBasic)game.enemyList[i]).isInScreen())
                        //{
                        //    DX.PlaySoundMem(((CBasic)game.enemyList[i]).destroySoundHandle,
                        //        DX.DX_PLAYTYPE_BACK);
                        //}
                        game.playerList.RemoveAt(i);
                    }
                }
                //Removing enemies

                for (int i = game.enemyList.Count - 1; i >= 0; i--)
                {
                    if (((CBasic)game.enemyList[i]).isAlive == false)
                    {
                        if ( ((CBasic)game.enemyList[i]).isInScreen() )
                        {
                            DX.PlaySoundMem(((CBasic)game.enemyList[i]).destroySoundHandle,
                                DX.DX_PLAYTYPE_BACK);
                        }
                        game.enemyList.RemoveAt(i);
                    }
                }



                //////////////////////////Drawing

                // Clear screen
                DX.ClearDrawScreen();


                // Draw player and its bullets
                foreach (CBasic pc in game.playerList)
                {
                    pc.draw();
                }


                // Draw enemies
                foreach (CBasic en in game.enemyList)
                {
                    en.draw();
                }

                // Draw the score TEST
                /*
                if (game.playerList.Count > 1)
                {
                    topleft = ((CBasic)game.playerList[1]).isAlive.ToString();
                    topleft += " Enemy#: " + (game.enemyList.Count).ToString();
                    topleft += " Player#: " + (game.playerList.Count).ToString();
                }*/

                topleft = " Enemy#: " + (game.enemyList.Count).ToString();



                DX.DrawString(0, 0, topleft, DX.GetColor(255, 255, 255));


                // Swap screens
                DX.ScreenFlip();

            }

            

            //DX.WaitKey();				// waiting key event
            DX.DxLib_End();				// DX library terminates

            return ;				// quit 

        }

        public static bool isCollision(CBasic character1, CBasic character2)
        {
            if (character1.HitBoxABS.bottomRight.x > character2.HitBoxABS.topLeft.x
            && character1.HitBoxABS.topLeft.x < character2.HitBoxABS.bottomRight.x
            && character1.HitBoxABS.bottomRight.y > character2.HitBoxABS.topLeft.y
            && character1.HitBoxABS.topLeft.y < character2.HitBoxABS.bottomRight.y
               )
            { return true; }

            return false;
        }
    }
}
