using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Slayter
{
    public class TradeScreen:ScreenOrigin
    {
        const int FirstHeroBlockX = 236;
        const int FirstHeroBlockY = 43;
        const int offsetHeight = 77;
        const int offsetWidth = 101;
        const int offsetForArrowsFirst = -39;
        const int offsetForArrowsSecond = 174;

        Texture2D singleRight;
        Texture2D singleLeft;
        Texture2D doubleRight;
        Texture2D doubleLeft;
        Texture2D allRight;
        Texture2D allLeft;

        Hero hero1;
        Hero hero2;

        List<MonsterLabel> labels;

        List<IDrawable> hero1Draw;
        List<IDrawable> hero2Draw;

        List<IClickable> hero1Click;
        public List<IClickable> hero2Click;

        public TradeScreen(Game1 game)
            :base(game)
        {

        }

        public TradeScreen(Screen screen,Hero hero1,Hero hero2)
            :base(screen.parent)
        {
            this.hero1 = hero1;
            this.hero2 = hero2;

            commingFrom = screen;

            hero1Draw = new List<IDrawable>();
            hero2Draw = new List<IDrawable>();

            hero1Click = new List<IClickable>();
            hero2Click = new List<IClickable>();

            singleRight=parent.Content.Load<Texture2D>("SingleArrowRight");
            singleLeft=parent.Content.Load<Texture2D>("SingleArrowLeft");
            doubleRight = parent.Content.Load<Texture2D>("DoubleArrowRight");
            doubleLeft=parent.Content.Load<Texture2D>("DoubleArrowLeft");
            allRight = parent.Content.Load<Texture2D>("BlockArrowRight");
            allLeft = parent.Content.Load<Texture2D>("BlockArrowLeft");

            labels = new List<MonsterLabel>();
            drawables = new List<IDrawable>(screen.Drawables);
            parent.setMessageScreen(this);

            Texture2D tradeTex = parent.Content.Load<Texture2D>("TradePanel");
            Rectangle tradeBox = new Rectangle((640 - tradeTex.Width )/ 2,( 480 - tradeTex.Height )/ 2,
                tradeTex.Width, tradeTex.Height);
            ClickableObject message = new ClickableObject(tradeTex, tradeBox, delegate(MouseState state,ClickableObject o) { });
            drawables.Add(message);

            DrawHeros();

            ClickableObject ground = new ClickableObject(screen.BGROUND.Picture, new Rectangle(0, 0, 640, 480), delegate(MouseState state, ClickableObject o) { 
                parent.removeMessageScreen(this);
                hero1.CheckPlayer();
                hero2.CheckPlayer();
            });
            
            clickables.Add(message);
            clickables.Add(ground);
        }

        public override void draw(Game1 game, SpriteBatch spriteBatch)
        {
            foreach(IDrawable element in drawables){
                element.draw(spriteBatch);
            }
            foreach (IDrawable element in hero1Draw)
            {
                element.draw(spriteBatch);
            }
            foreach (IDrawable element in hero2Draw)
            {
                element.draw(spriteBatch);
            }
        }
        
        public override void click(Game1 game, MouseState state)
        {
            if (CheckForClickableObject(game, state, hero1Click))
            {
                return;
            }
            if (CheckForClickableObject(game, state, hero2Click))
            {
                return;
            }
            CheckForClickableObject(game, state, clickables);
        }

        private void DrawHeros()
        {


            hero1Draw = new List<IDrawable>();
            hero2Draw = new List<IDrawable>();

            hero1Click = new List<IClickable>();
            hero2Click = new List<IClickable>();
            labels = new List<MonsterLabel>();
            drawHeroMonsters(hero1, 0, offsetHeight, offsetForArrowsFirst, false,hero1Draw);
            drawHeroMonsters(hero2, offsetWidth, offsetHeight, offsetForArrowsSecond, true,hero2Draw);
        }

        private void drawHeroMonsters(Hero hero,int offsetX,int offsetY,int offsetArrows,bool inversed,List<IDrawable> list)
        {
            SpriteFont font = parent.Content.Load<SpriteFont>("font");
            for (int i = 0; i < hero.units.Count; i++)
            {
                Monster monster = hero.units[i];
                Texture2D mon = monster.Texture;
                Rectangle rect = new Rectangle(FirstHeroBlockX+offsetX, FirstHeroBlockY+offsetY*i, mon.Width, mon.Height);
                Background mons = new Background(mon, rect);
                MonsterLabel count = new MonsterLabel(monster,font, monster.Quantity + "", new Vector2(rect.X, rect.Y + (rect.Width - (font.MeasureString("1").X+5))));

                labels.Add(count);
                list.Add(mons);
                list.Add(count);

                Texture2D arr;
                if (inversed)
                    arr = singleLeft;
                else
                    arr = singleRight;
                Rectangle r = new Rectangle(FirstHeroBlockX+offsetArrows,FirstHeroBlockY + offsetY*i,arr.Width,arr.Height);
                ClickableObject singleArrow = new ClickableObject(arr, r, delegate(MouseState state, ClickableObject o) {exchange(hero, monster,1);});
                addToHeroLists(hero,singleArrow);

                if (inversed)
                    arr = doubleLeft;
                else
                    arr = doubleRight;
                r = new Rectangle(r.X, r.Y + r.Height, arr.Width, arr.Height);
                ClickableObject doubleArrow = new ClickableObject(arr, r, delegate(MouseState state, ClickableObject o) { exchange(hero, monster, monster.Quantity/2); });
                addToHeroLists(hero,doubleArrow);

                if (inversed)
                    arr = allLeft;
                else
                    arr = allRight;
                r = new Rectangle(r.X, r.Y + r.Height, arr.Width, arr.Height);
                ClickableObject allArrow = new ClickableObject(arr, r, delegate(MouseState state, ClickableObject o) { exchange(hero, monster, monster.Quantity); });
                addToHeroLists(hero,allArrow);
            }
        }

        private void exchange(Hero hero, Monster monster,int count)
        {
            int x = otherHero(hero).units.Count;
            if (monster.removeCount(count) && otherHero(hero).addMonster(monster, count))
            {
                if (hero.CheckMonsters() || x!=otherHero(hero).units.Count)
                {
                    DrawHeros();
                }
                else
                {
                    RefreshLabels();
               }
            }
        }

        private Hero otherHero(Hero h){
            if (h == hero1)
            {
                return hero2;
            }
            return hero1;
        }

        private void RefreshLabels(){
            foreach (MonsterLabel element in labels)
            {
                element.Refresh();
            }
        }

        protected void addToHeroLists(Hero hero,ClickableObject b)
        {
            if (hero == hero1)
            {
                hero1Draw.Add(b);
                hero1Click.Add(b);
            }
            else
            {
                hero2Draw.Add(b);
                hero2Click.Add(b);
            }
        }

        protected void removeFromHeroLists(Hero hero,ClickableObject b)
        {
            if (hero == hero1)
            {
                if (hero1Draw.Contains(b))
                    hero1Draw.Remove(b);
                if (hero1Click.Contains(b))
                    hero1Click.Remove(b);
            }
            else
            {
                if (hero2Draw.Contains(b))
                    hero2Draw.Remove(b);
                if (hero2Click.Contains(b))
                    hero2Click.Remove(b);
            }
        }
    }
}
