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

public class SkillExplanation {

    private string skillInfoFilePath = "Data/SkillInfo";//信息文件路径，在Resources下
    public DataTable skillInfodt = new DataTable();//物品信息表

    public Sprite SkillNum;

    public SkillExplanation()
    {
        SkillInfo_csv();
    }

    public string Explanation(int id, string value)
    {
        return skillInfodt.Select("ID = " + id)[0][value].ToString();
    }

    public bool IsSkillExist(int CharacterID, string SkillNum)
    {
        if (skillInfodt.Select("CharacterID = " + CharacterID + " and SkillNum = " + SkillNum).Length >0)
        {
            return true;
        }else
        {
            return false;
        }
    }
    public bool IsSkillExist(int CharacterID, int SkillNum)
    {
        if (skillInfodt.Select("CharacterID = " + CharacterID + " and SkillNum = " + SkillNum).Length > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public string Explanation(int CharacterID, int SkillNum, string value)
    {
        return skillInfodt.Select("CharacterID = " + CharacterID + " and SkillNum = " + SkillNum)[0][value].ToString();
    }
    public Sprite SkillImage(int CharacterID, string SkillNum)
    {
        string path =  skillInfodt.Select("CharacterID = " + CharacterID + " and SkillNum = " + SkillNum)[0]["Image"].ToString();

        Texture2D Image_tmp = (Texture2D)Resources.Load("SkillsImage/" + path) as Texture2D;
        if (Image_tmp == null)
        {
            Image_tmp = (Texture2D)Resources.Load("SkillsImage/SkillNum" + SkillNum) as Texture2D;
        }
        return Sprite.Create(Image_tmp, new Rect(0, 0, Image_tmp.width, Image_tmp.height), new Vector2(0.5f, 0.5f));
        //return Resources.Load("SkillsImage/" + path , typeof(Sprite)) as Sprite;
    }
    public Sprite SkillImage(int CharacterID, int SkillNum)
    {
        string path = skillInfodt.Select("CharacterID = " + CharacterID + " and SkillNum = " + SkillNum)[0]["Image"].ToString();

        Texture2D Image_tmp = (Texture2D)Resources.Load("SkillsImage/" + path) as Texture2D;
        if (Image_tmp == null)
        {
            Image_tmp = (Texture2D)Resources.Load("SkillsImage/SkillNum" + SkillNum) as Texture2D;
        }
        return Sprite.Create(Image_tmp, new Rect(0, 0, Image_tmp.width, Image_tmp.height), new Vector2(0.5f, 0.5f));
        //return Resources.Load("SkillsImage/" + path , typeof(Sprite)) as Sprite;
    }

    private void SkillInfo_csv()//载入文件：信息表
    {
        if (skillInfodt.Rows.Count == 0)
        {
            TextAsset reader = Resources.Load(skillInfoFilePath, typeof(TextAsset)) as TextAsset;

            string[] lineArray = reader.text.Split("\r"[0]);
            for (int m = 0; m < lineArray.Length; m++)
            {
                if (m < 1)
                {
                    string[] split = lineArray[m].Split(',');
                    for (int i = 0; i < split.Length; i++)
                    {
                        if (split[i].StartsWith("Name") || split[i].StartsWith("Info") || split[i] == "Trajectory" || split[i] == "Image")
                        {
                            skillInfodt.Columns.Add(split[i], System.Type.GetType("System.String"));
                        }
                        else if(split[i] == "Random")
                        {
                            skillInfodt.Columns.Add(split[i], System.Type.GetType("System.Single"));
                        }
                        else 
                        {
                            skillInfodt.Columns.Add(split[i], System.Type.GetType("System.Int32"));
                        }
                    }
                }
                else if (lineArray[m] != "\n")
                {
                    string[] split = lineArray[m].Split(',');
                    DataRow dr = skillInfodt.NewRow();
                    int j = 0;
                    for (int i = 0; i < skillInfodt.Columns.Count; i++)
                    {
                        string str = split[j];
                        while (Tools_csv.CountQuotation(str))
                        {
                            j++;
                            str += "," + split[j];
                        }
                        str.Replace("\"\"", "\"");
                        if (str.StartsWith("\"") && str.EndsWith("\""))
                        {
                            str = str.Substring(1, str.Length - 2);
                        }
                        j++;
                        dr[i] = str;
                    }
                    skillInfodt.Rows.Add(dr);
                }
            }
        }
    }

}
