﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Generator : LargeBlock {
        
    protected float efficiency;

    IPoint intputOffset;
    bool isStateChanged;

    int chargeLevel;

    public Generator(int id, GameObject parentObject, GameObject blockObject)
        : base(id, parentObject, blockObject)
    {
        initBlock("generator", "power", true);

        efficiency = 0.9f;

        intputOffset = new IPoint(0, 1);

        chargeLevel = 0;
    }

    public override Block clone(GameObject parentObject, BlocksManager blocksManager, GameObject blockObject)
    {
        Generator block = new Generator(blockId, parentObject, blockObject);
        block.initSolidBlock(blocksManager, "ironLiquid", 1535, 1400);
        block.initLargeBlock(new IPoint(4, 3));
        return block;
    }

    public override SyntInfo[] getSyntInfo(BlocksManager blocksManager)
    {
        int[,] syntData = new int[3, 2] { 
                    { blocksManager.winding.getId(), 4 }, 
                    { blocksManager.copperWire.getId(), 7 },                     
                    { blocksManager.iron.getId(), 15 }
        };

        SyntInfo[] syntInfos = new SyntInfo[1];
        syntInfos[0] = new SyntInfo(this, syntData, "power", 12);

        return syntInfos;
    }

    public override string getDetalContent()
    {
        string content = base.getDetalContent();
        content += "\n" + ILang.get("efficiency", "inc") + ":" + efficiency;
        return content;
    }

    public override void onAssembleFinished(BlocksEngine blocksEngine)
    {
        if (m_faceDir == Dir.left)
        {
            intputOffset = new IPoint(3, 1);
        }
        else
        {
            intputOffset = new IPoint(0, 1);
        }
    }

    public override void updata(BlocksEngine blocksEngine)
    {
        base.updata(blocksEngine);

        if (m_isBroken == false)
        {
            checkStateRule(blocksEngine);
        }
    }

    protected void checkStateRule(BlocksEngine blocksEngine)
    {
        
        if (isStateChanged)
        {
            isStateChanged = false;
            if (isOrigin())
            {
                Generator btnBlock = blocksEngine.getBlock(getReviseBlockCoor(new IPoint(0, 1))) as Generator;
                if (btnBlock != null)
                {
                    if (chargeLevel == 0)
                    {
                        btnBlock.setSpriteRect(4);                     
                    }
                    else
                    {
                        btnBlock.setSpriteRect(11 + chargeLevel);                        
                    }
                }
            }
        }        
    }

    public override void threadUpdata(BlocksEngine blocksEngine)
    {
        if (chargeLevel > 0)
        {
            isLight = true;
            setLightLevelOnThread(10 + chargeLevel * 5, Color.yellow);
        }
        else
        {
            isLight = false;
        }
        base.threadUpdata(blocksEngine);
    }    

    public override void onReciverMe(BlocksEngine blocksEngine, float me, int putterDir, Block putter)
    {
        if (m_isBroken == false)
        {
            me = Mathf.Abs(me);
            if (getOffset().equal(intputOffset))
            {
                Block block =  getOrgBlock(blocksEngine);
                if (block != null)
                {
                    Generator orgBlock = block as Generator;
                    if (me <= 90)
                        orgBlock.setChargeLevel(0);
                    else if (me > 90 && me <= 300)
                        orgBlock.setChargeLevel(1);
                    else if (me > 300 && me <= 700)
                        orgBlock.setChargeLevel(2);
                    else if (me > 700 && me <= 1200)
                        orgBlock.setChargeLevel(3);
                    else if (me > 1200)
                        orgBlock.setChargeLevel(4);
                }                         

                blocksEngine.chargeElectric(this, me * ElectricManager.me2elCoefficient * efficiency);
            }
        }
    }

    public void setChargeLevel(int level)
    {
        int lastChargeLevel = chargeLevel;
        chargeLevel = level;       
        if (lastChargeLevel != chargeLevel)
        {
            isStateChanged = true;
        }
    }

    public override void onWorldModeChunkMove(World world, BlocksEngine blocksEngine)
    {
        onAssembleFinished(null);
    }

    public override bool isCollider(Player player)
    {
        return false;
    }

    public override bool isTransparent()
    {
        return true;
    }
}
