using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
[System.Serializable]
public class AreaInfoUseData
{
    public short id;
    public short clickX;
    public short clickY;
}
public class GameScenUserInfo
{
    static public string RunGameDatasRootName = "gameData";
    public string img = "";//线图
    public string json = "";//数据
    public string finishImg = "";//完成图
    public string id = "";//int 的id.toString()
    public string file = "";//zip url
    //以上是url
    public int status = 0;//0 未开始 1 这个id进过游戏 2 至少有一个区域点亮 3完成
    public double progress = 0;//0 -- 1 具体自己算百分比
    public List<AreaInfoUseData> areaInfoUseData = new List<AreaInfoUseData>();
    private string getUrlByLocalPath(string url)
    {
        if (url.StartsWith("http") == false) return "";
        int idx = url.LastIndexOf("/");
        int idx1 = url.LastIndexOf(".");
        if (idx < 0 || idx1 < idx)
        {
            return "";
        }
        string localName = url.Substring(idx, idx1 - idx).Trim();
        if (localName.Length == 0) return "";
        if (localName.StartsWith("/")) localName = localName.Substring(1);
        string ret = utilsTools.getWritableByPath(RunGameDatasRootName+"/" + localName);
        ret = ret + url.Substring(idx1);
        return ret;
    }
    public static string getAllRoot()
    {
        return utilsTools.getWritableByPath(RunGameDatasRootName);
    }
    public string getLineImgLocalPath()
    {
        return getUrlByLocalPath(img);
    }
    public string getColorImgLocalPath()
    {
        return getUrlByLocalPath(finishImg);
    }
    public string getJsonLocalPath()
    {
        return getUrlByLocalPath(json);
    }
    public string getLocalThumbnailPath()
    {
        string path = utilsTools.getWritableByPath(RunGameDatasRootName + "Thumbnail");
        utilsTools.CreateDirectory(path);
        path = path + "/" + id + ".png";
        return path;
    }
    public bool createWorkDirectory()
    {
        string path = getLocalThumbnailPath();
        if (path.Equals("")) return false;
        int idx = path.LastIndexOf("/");
        string dirpath = path.Substring(0, idx);
        
        utilsTools.CreateDirectory(dirpath);
        return true;
    }
    //format 后出现目录，自己在外面控制这里不做判定
    public string getPathByFormath(string format, params System.Object[] args)
    {
        string name = string.Format(format, args);
        string ret = utilsTools.getWritableByPath(RunGameDatasRootName + "/"+ id+"/" + name);
        return ret;
    }
    public string getAllShowAreaId()
    {
        string ret = "";
        for (int i = 0; i < this.areaInfoUseData.Count; i++)
        {
            short id = this.areaInfoUseData[i].id;
            ret = ret + "," + id.ToString();
        }
        if (ret.StartsWith(","))
        {
            ret = ret.Substring(1);
        }
        return ret;
    }
    public void setShowAreaids(string areaids)
    {
        string[] ids = areaids.Split(",");
        Dictionary<short, bool> existIds = new Dictionary<short, bool>();
        foreach (var item in areaInfoUseData)
        {
            existIds.Add(item.id, true);
        }
        for (int i = 0; i < ids.Count(); i++)
        {
            short id = Convert.ToInt16(ids[i]);
            if(id >= 0 && existIds.ContainsKey(id) == false)
            {
                AreaInfoUseData areaData = new AreaInfoUseData();
                areaData.id = id;
                areaData.clickX = -1;
                areaData.clickY = -1;
                areaInfoUseData.Add(areaData);
                existIds.Add(id, true);
            }
        }
        existIds.Clear();
    }
}
