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

[Serializable]
public class BeatSageRoot
{
    public string _version;
    public List<BeatSageNote> _notes;
}

[Serializable]
public class BeatSageNote
{
    public float _time;
    public int _lineIndex;
    public int _lineLayer;
    public int _type;
    public int _cutDirection;
}

public static class BeatSageConverter
{
    public static List<NoteData> Convert(string rawJson, float bpm)
    {
        var result = new List<NoteData>();

        BeatSageRoot sageData = JsonUtility.FromJson<BeatSageRoot>(rawJson);
        if (sageData?._notes == null)
        {
            Debug.LogWarning("[BeatSageConverter] 파싱 실패 또는 노트 없음.");
            return result;
        }

        foreach (var note in sageData._notes)
        {
            // 일반 노트(0: 빨강, 1: 파랑)만 처리, 폭탄(3) 등 제외
            if (note._type != 0 && note._type != 1) continue;

            result.Add(new NoteData
            {
                time      = (note._time * 60f) / bpm,
                position  = note._lineIndex,
                colorType = note._type
            });
        }

        Debug.Log($"[BeatSageConverter] 변환 완료: {result.Count}개 노트");
        return result;
    }

    public static string ToMapJson(List<NoteData> notes)
    {
        var mapData = new MapData { target = notes };
        return JsonUtility.ToJson(mapData, true);
    }
}
