using Otter;
using Puppyups.Utilities;
using System;

namespace Puppyups.objects
{
    class Chain : Entity
    {
        private Spritemap<string> _spriteMap;
        private Grapple _marriedEntity;
        private Grapple2 _marriedEntity2;
        private bool one = true;
        private bool canDieByGrapple;
        private bool plat;
        private bool silver;
        private bool gold;

        public Chain(float x, float y, Grapple e, Grapple2 e2)
        {
            X = x; Y = y; _marriedEntity = e; _marriedEntity2 = e2;
            SetHitbox(10, 10, (int)Global.Type.CHAIN);
            Collider.X = -5;
            Collider.Y = -5;
            _spriteMap = new Spritemap<string>(Assets.CHAIN, 10, 10);
            _spriteMap.Add("main", new int[] { 0, 1, 2, 3 }, new float[] { 10f, 10f });
            _spriteMap.Play("main");
            _spriteMap.CenterOrigin();
            if (Rand.Int(0, 2) == 1) //50% chance
            {
                _spriteMap.Color = Color.Yellow;
                gold = true;
            }
            else if (Rand.Int(0, 2) == 1)//50% chance
            {
                _spriteMap.Color = Color.Grey;
                silver = true;
            }
            else
            {
                _spriteMap.Color = Color.White;
                plat = true;
            }
            Graphic = _spriteMap;
        }
        public override void Update()
        {
            base.Update();
            if (_marriedEntity == null && _marriedEntity2 == null)//maybe...
                RemoveSelf();
            if (!AmICollidingWithMyFriends(Collider) && one && _marriedEntity != null)
            {
                one = false;
                canDieByGrapple = true;
                _marriedEntity.SpawnChain = true;
            }
            else if (!AmICollidingWithMyFriends2(Collider) && one && _marriedEntity2 != null)
            {
                one = false;
                canDieByGrapple = true;
                _marriedEntity2.SpawnChain = true;
            }
            var p = Collider.Collide(X, Y, (int)Global.Type.PLAYER);
            if (p != null)
            {//the player should be able to remove the chain if he gets pulled into it.
                if (plat)
                {
                    SoundManager.PlayRequestedSound("_platcoin");
                    Global.Player1PlatRingsCollected++;
                    Global.score += 4;
                }
                else if (silver)
                {
                    SoundManager.PlayRequestedSound("_silvercoin");
                    Global.Player1SilverRingsCollected++;
                    Global.score += 2;
                }
                else if (gold)
                {
                    SoundManager.PlayRequestedSound("_goldcoin");
                    Global.Player1GoldRingsCollected++;
                    Global.score += 3;
                }
                RemoveSelf();
            }
            var p2 = Collider.Collide(X, Y, (int)Global.Type.PLAYER2);
            if (p2 != null)
            {//the player2 should be able to remove the chain if he gets pulled into it.
                if (plat)
                {
                    SoundManager.PlayRequestedSound("_platcoin");
                    Global.Player2PlatRingsCollected++;
                    Global.score2 += 4;
                }
                else if (silver)
                {
                    SoundManager.PlayRequestedSound("_silvercoin");
                    Global.Player2SilverRingsCollected++;
                    Global.score2 += 2;
                }
                else if (gold)
                {
                    SoundManager.PlayRequestedSound("_goldcoin");
                    Global.Player2GoldRingsCollected++;
                    Global.score2 += 3;
                }
                RemoveSelf();
            }
            if (canDieByGrapple && _marriedEntity != null)
            {//trying to only have the one that spawned the chain can kill the chain.
                var friend = Collider.Collide(X, Y, (int)Global.Type.GRAPPLE);
                if (friend != null)
                {
                    if (_marriedEntity.InstanceId == friend.Entity.InstanceId)
                        RemoveSelf();
                }
            }
            else if (canDieByGrapple && _marriedEntity2 != null)
            {//trying to only have the one that spawned the chain can kill the chain.
                var friend = Collider.Collide(X, Y, (int)Global.Type.GRAPPLE2);
                if (friend != null)
                {
                    if (_marriedEntity2.InstanceId == friend.Entity.InstanceId)
                        RemoveSelf();
                }
            }
        }

        public bool AmICollidingWithMyFriends(Collider c)
        {
            var g = c.Collide(X, Y, (int)Global.Type.GRAPPLE);
            if (g != null)
                return true;
            else
                return false;
        }

        public bool AmICollidingWithMyFriends2(Collider c)
        {
            var g = c.Collide(X, Y, (int)Global.Type.GRAPPLE2);
            if (g != null)
                return true;
            else
                return false;
        }

        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Blue);
        }
    }
}
