using UnityEngine;
using System;
using System.Collections;

abstract class Note
{
    // define common note properties

    public double Time;
    public HandType HandType;

    protected NoteObject noteObject;

    // interpret note option
    protected abstract void FromBmsNum(String num);
    protected abstract NoteObject CreateNoteObjectImpl();

    // instantiate associated game object

    public void CreateNoteObject()
    {
        noteObject = CreateNoteObjectImpl();
    }

    public void Update(float remainingTime)
    {
        if (remainingTime < 0 || remainingTime > 2)
        {
            noteObject.gameObject.SetActive(false);
        }
        else
        {
            noteObject.gameObject.SetActive(true);
        }
        noteObject.SetPosition(remainingTime);
    }

    // factory method
    // may return null if given note is not supported
    public static Note Create(BmsNote bn, double bpm)
    {
        Note note = null;
        HandType handType = HandType.None;

        switch (bn.lane)
        {
            case "11": // 1p first lane: FORWARD LEFT HAND
                note = new ForwardNote();
                handType = HandType.Left;
                break;
            case "12": // 1p second lane: FORWARD RIGHT HAND
                note = new ForwardNote();
                handType = HandType.Right;
                break;
            //case "13": // 1p third lane: REAR LEFT HAND
            //    handType = HandType.Left;

            //    break;
            //case "14": // 1p forth lane: REAR RIGHT HAND
            //    handType = HandType.Right;

            //    break;
            //case "15": // 1p fifth lane: EDGE LEFT HAND
            //    handType = HandType.Left;

            //    break;
            //case "16": // 1p sixth lane: EDGE RIGHT HAND
            //    handType = HandType.Right;

            //    break;

            // add action notes 

            default:
                return null;
        }

        note.FromBmsNum(bn.num);
        note.HandType = handType;
        note.Time = bn.barTime * (60.0 / bpm * 4);

        note.CreateNoteObject();

        return note;
    }
}

class ForwardNote : Note
{
    private float x, y;

    protected override NoteObject CreateNoteObjectImpl()
    {
        var obj = MonoBehaviour.Instantiate<ForwardNoteObject>(PlayEngine.inst.ForwardNoteObject);
        obj.Init(x, y, HandType);
        return obj;
    }

    protected override void FromBmsNum(string num)
    {
        Debug.Log(num);
        // since 00 is used for empty notes, 1 ~ Z is valid range
        x = charToCoord(num[0]) / 35.0f;
        y = charToCoord(num[1]) / 35.0f;
    }

    private int charToCoord(char a)
    {
        if (char.IsDigit(a))
        {
            return a - '1';
        }
        else if (char.IsLetter(a))
        {
            return char.ToLower(a) - 'a' + 9;
        }

        throw new ArgumentException("wrong data");
    }
}

class RearNote : Note
{
    protected override NoteObject CreateNoteObjectImpl()
    {
        throw new NotImplementedException();
    }

    protected override void FromBmsNum(string num)
    {
        throw new NotImplementedException();
    }
}

class EdgeNote : Note
{
    protected override NoteObject CreateNoteObjectImpl()
    {
        throw new NotImplementedException();
    }

    protected override void FromBmsNum(string num)
    {
        throw new NotImplementedException();
    }
}

public enum NoteType
{
    Front,
    Rear,
    Edge,
    Action
}

public enum HandType
{
    Left,
    Right,
    None
}