﻿using System;
using System.Text;

namespace SoccerSimulation {
    public class PlayerDecideSource {

        private static string GetDefaultDecide() {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("            if (HasBall()) {");
            sb.AppendLine("                KickBallTowardsGoal();");
            sb.AppendLine("            }");
            sb.AppendLine("            else if (IsClosestToBall() || (!IsBallOurs() && IsFirst())) {");
            sb.AppendLine("                MoveTowardsBall();");
            sb.AppendLine("            }");
            sb.AppendLine("            else if (IsBallOurs()) {");
            sb.AppendLine("                MoveTowardsEnemyGoal();");
            sb.AppendLine("            }");
            sb.AppendLine("            else if (IsLast()) {");
            sb.AppendLine("                MoveTowardsOurGoal();");
            sb.AppendLine("            }");
            sb.AppendLine("            else {");
            sb.AppendLine("                MoveTowardsBall();");
            sb.AppendLine("            }");

            return sb.ToString();
        }

        public static string GetSourceCode(string code) {
            StringBuilder sb = new StringBuilder();

            code = code.Equals(String.Empty) ? GetDefaultDecide() : code;

            sb.AppendLine("using System;");
            sb.AppendLine("using System.Drawing;");
            sb.AppendLine("");
            sb.AppendLine("namespace SoccerSimulation {");
            sb.AppendLine("    public class PlayerDecide {");
            sb.AppendLine("");
            sb.AppendLine("        private PlayerLogic PlayerLogic { get; set; }");
            sb.AppendLine("        private Player player;");
            sb.AppendLine("");
            sb.AppendLine("        public PlayerDecide() {");
            sb.AppendLine("            PlayerLogic = null;");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        public void Decide(Player p) {");
            sb.AppendLine("            player = p;");
            sb.AppendLine("            try {");
            sb.AppendLine(code);
            sb.AppendLine("            } catch (Exception ex) {");
            sb.AppendLine("                 Console.WriteLine(ex.Message);");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        public void InstallPlayerLogic(PlayerLogic pl) {");
            sb.AppendLine("            PlayerLogic = pl;");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void KickBallTowardsGoal() {");
            sb.AppendLine("            PlayerLogic.KickBallTowardsGoal(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private bool HasBall() {");
            sb.AppendLine("            return PlayerLogic.CollisionBall(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private bool IsClosestToBall() {");
            sb.AppendLine("            return PlayerLogic.IsClosestToBall(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private bool IsBallOurs() {");
            sb.AppendLine("            return PlayerLogic.IsBallOurs(player.Team);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private bool IsLast() {");
            sb.AppendLine("            return PlayerLogic.IsLast(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private bool IsFirst() {");
            sb.AppendLine("            return PlayerLogic.IsFirst(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void MoveTowardsOurGoal() {");
            sb.AppendLine("            PlayerLogic.MoveTowardsOurGoal(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void MoveTowardsEnemyGoal() {");
            sb.AppendLine("            PlayerLogic.MoveTowardsEnemyGoal(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void PassBall() {");
            sb.AppendLine("            PlayerLogic.PassBall(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void MoveTowardsBall() {");
            sb.AppendLine("            PlayerLogic.MoveTowardsBall(player);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        private void Stand() {");
            sb.AppendLine("            PlayerLogic.Stand(player);");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("}");

            String classCode = sb.ToString();

            return classCode;
        }
    }
}
