using UnityEngine;
using System.Collections;

public enum CropType {

    none,
    Turnip,
    Carrot,
    Strawberry,

    count
}

public class Field : MonoBehaviour {

    public Texture[] m_emission = null;
    public Sprite[] m_turnip = null;
    public Sprite[] m_turnipRotting = null;
    public Sprite[] m_carrot = null;
    public Sprite[] m_carrotRotting = null;
    public Sprite[] m_strawberry = null;
    public Sprite[] m_strawberryRotting = null;
    public Sprite[] m_basket = null;
    public ShowHideButton m_basketButton = null;

    public CropType m_wanted = CropType.none;
    public CropType m_planted = CropType.none;
    public GrowingThing[] m_crops = null;
    public Timer m_appearTimer = new Timer( );
    public Renderer m_renderer = null;
    private Material m_material = null;
    private Timer m_basketTimer = new Timer( );
    private int m_basketIndex = 0;
    public bool m_flaged = false;
    public Texture m_plantable = null;
    public Texture m_notPlantable = null;

    void Start( ) {
        m_material = m_renderer.material;
    }

    public void reset( ) {

        if ( !isFullyDead( ) ){

            for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {
                m_crops[ cropIndex ].initialize( );
            }

            if ( m_material == null ) {
                m_material = m_renderer.material;
            }

            m_material.SetColor( "_Color", new Color( 0, 0, 0, 0.0f ) );
            m_appearTimer = new Timer( );
            m_basketTimer = new Timer( );
            m_basketIndex = 0;
            m_flaged = false;
            m_wanted = CropType.none;
            m_planted = CropType.none;
            m_material.mainTexture = m_plantable;
            m_material.SetTexture( "_EmissionMap", m_plantable );
        }
    }

    void Update( ) {

        m_appearTimer.step( );

        if ( m_appearTimer.isRunning( ) ) {
            m_material.SetColor("_Color", new Color(0, 0, 0, Mathf.Lerp( 0.0f, 1.0f, m_appearTimer.getPercentage( ) ) ) );
        }

        for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {
            Main.get( ).spriteOrientation(  m_crops[ cropIndex ].transform );
        }

        Main.get( ).spriteOrientation( m_basketButton.transform.parent );

        m_basketTimer.step( );

        if ( m_basketTimer.isEndedOnce( ) ) {

            m_basketIndex++;

            if ( m_basketIndex >= m_basket.Length ) {
                m_basketIndex = 0;
            }

            m_basketButton.m_image.sprite = m_basket[ m_basketIndex ];
            m_basketTimer.start( 0.05f );
        }

        isFullyDead( );
    }

    public void plant( CropType crop ) {

        m_planted = crop;

        for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {

            switch ( crop ) {

                case CropType.Turnip: {
                    m_crops[ cropIndex ].m_emission = m_emission[ 0 ];
                    m_crops[ cropIndex ].m_sprites = m_turnip;
                    m_crops[ cropIndex ].m_rotting = m_turnipRotting;
                } break;

                case CropType.Carrot: {
                    m_crops[ cropIndex ].m_emission = m_emission[ 1 ];
                    m_crops[ cropIndex ].m_sprites = m_carrot;
                    m_crops[ cropIndex ].m_rotting = m_carrotRotting;
                } break;

                case CropType.Strawberry: {
                    m_crops[ cropIndex ].m_emission = m_emission[ 2 ];
                    m_crops[ cropIndex ].m_sprites = m_strawberry;
                    m_crops[ cropIndex ].m_rotting = m_strawberryRotting;
                } break;
            }

            m_crops[ cropIndex ].startGrowing( Random.Range( 5.0f, 10.0f ) );
        }
    }

    public void appear( ) {
        m_appearTimer.start( 0.25f );
    }

    public bool isReadyToHarvest( ) {

        bool result = true;

        for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {

            result = m_crops[ cropIndex ].isFullyGrown( );

            if ( !result ) {
                break;
            }
        }

        return result;
    }

    public bool isFullyDead( ) {

        bool result = true;

        for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {

            result = m_crops[ cropIndex ].isDead( );

            if ( !result ) {
                break;
            }
        }

        if ( result ) {
            m_material.mainTexture = m_notPlantable;
            m_material.SetTexture( "_EmissionMap", m_notPlantable );
            hideBasketIcon( );
        }



        return result;
    }

    public int harvest( ) {

        int result = 0;

        for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {

            if ( !m_crops[ cropIndex ].isDead( ) ) {
                result++;
            }

            m_crops[ cropIndex ].gameObject.SetActive( false );
        }

        m_material.mainTexture = m_notPlantable;
        m_material.SetTexture( "_EmissionMap", m_notPlantable );

        m_basketButton.hide( );

        return result;
    }

    public void setBasketIcon( ) {

        if ( m_crops[ 0 ].gameObject.activeInHierarchy ) {

            m_basketButton.show( );
            m_basketTimer.start( 0.05f );
            m_basketIndex = 0;
            m_basketButton.m_image.sprite = m_basket[ m_basketIndex ];
            m_flaged = true;

            for ( int cropIndex = 0; cropIndex < m_crops.Length; cropIndex++ ) {
                m_crops[ cropIndex ].startRotting( Random.Range( 10.0f, 20.0f ) );
            }
        }
    }

    public void hideBasketIcon( ) {
        m_basketButton.hide( );
    }
}
