using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class GameDataService : Singleton<GameDataService>
{
    public PlayerVO myPlayer = new PlayerVO();
    public int[] answer = new int[20];
    public int[] questionList = new int[20];
    public GameDataService()
    {
        
    }
    public void SaveGameData()
    {

    }
    void Awake()
    {
        ClearPlayerData();
    }

    public void LogPlayerData()
    {
        Debug.LogFormat("Player money = {0},work = {1},honor={2},relationship={3}",myPlayer.money, myPlayer.work, myPlayer.honor, myPlayer.relationship);
    }

    public void ClearPlayerData()
    {
        Debug.LogFormat("ClearPlayerData()");
        myPlayer.playerName = "นิรนาม";
        myPlayer.playerCenter = "ศูนย์นิรนาม";
        myPlayer.playerTeacher = "ครูนิรนาม";
        myPlayer.playerAge = 3;
        myPlayer.money = 50;
        myPlayer.work = 50;
        myPlayer.honor = 50;
        myPlayer.relationship = 50;
        myPlayer.currentQuestion = 1;
        myPlayer.currentIndexQuestion = 0;
        myPlayer.playerAvatar = 0;
        myPlayer.isSave = false;
        ClearAnswer();
        questionList = GenerateQustionList();
    }

    void ClearAnswer()
    {
        for (int i = 0; i < answer.Length; i++)
        {
            answer[i] = 0;
        }
    }

    int[] GenerateQustionList()
    {
        List<int> result = new List<int>();

        int qPerRound = 4;
        int[] q1 = new int[qPerRound];
        int startValue = 1;
        for (int i = 0; i < q1.Length; i++)
        {
            q1[i] = i + startValue;
        }
        q1 = Shuffle(q1);
        for (int i = 0; i < q1.Length; i++)
        {
            result.Add(q1[i]);
        }

        int[] q2 = new int[qPerRound];
        startValue += qPerRound;
        for (int i = 0; i < q2.Length; i++)
        {
            q2[i] = i + startValue;
        }
        q2 = Shuffle(q2);
        for (int i = 0; i < q2.Length; i++)
        {
            result.Add(q2[i]);
        }

        int[] q3 = new int[qPerRound];
        startValue += qPerRound;
        for (int i = 0; i < q3.Length; i++)
        {
            q3[i] = i + startValue;
        }
        q3 = Shuffle(q3);
        for (int i = 0; i < q3.Length; i++)
        {
            result.Add(q3[i]);
        }

        int[] q4 = new int[qPerRound];
        startValue += qPerRound;
        for (int i = 0; i < q4.Length; i++)
        {
            q4[i] = i + startValue;
        }
        q4 = Shuffle(q4);
        for (int i = 0; i < q4.Length; i++)
        {
            result.Add(q4[i]);
        }

        int[] q5 = new int[qPerRound];
        startValue += qPerRound;
        for (int i = 0; i < q5.Length; i++)
        {
            q5[i] = i + startValue;
        }
        q5 = Shuffle(q5);
        for (int i = 0; i < q5.Length; i++)
        {
            result.Add(q5[i]);
        }
        string log = "Question List = ";
        for (int i = 0; i < result.Count; i++)
        {
           log += result[i]+",";
        }
        Debug.Log(log);
        return result.ToArray();
    }


    int[] Shuffle(int[]  input)
    {
        int[]  ts = input;
        int count = ts.Length;
        var last = count - 1;
        for (var i = 0; i < last; ++i)
        {
            var r = Random.Range(i, count);
            var tmp = ts[i];
            ts[i] = ts[r];
            ts[r] = tmp;
        }

        return ts;
    }
    public void ScoreProcess(int questionNo,int choiceNo)
    {
        answer[questionNo-1] = choiceNo;
        switch (questionNo)
        {
            case 1:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Work,PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 2:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 3:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 4:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 5:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 6:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 7:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 8:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 9:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 10:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 11:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 12:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 13:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                }
                break;
            case 14:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);

                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                }
                break;
            case 15:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);


                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);

                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                }
                break;
            case 16:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Direct);
                        break;
                }
                break;
            case 17:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 18:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
            case 19:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        break;
                }
                break;
            case 20:
                switch (choiceNo)
                {
                    case 1:
                        myPlayer.Plus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Indirect);
                        myPlayer.Plus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Plus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                    case 2:
                        myPlayer.Minus(PlayerVO.ScoreType.Money, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Work, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Honor, PlayerVO.Effect.Direct);
                        myPlayer.Minus(PlayerVO.ScoreType.Relationship, PlayerVO.Effect.Indirect);
                        break;
                }
                break;
        }
    }

    public void SaveData2File()
    {
        string filenName = string.Format("{0}.csv", System.DateTime.Now.ToString("yyyy"));
        string path = Application.dataPath + "/" + filenName;
        Debug.Log("SaveData >> " + path);
        //Header
        if (myPlayer.isSave == false)
        {
            if (!File.Exists(path))
            {
                using (StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.UTF8))
                {
                    string data = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\"," +
                        "\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\",\"{15}\",\"{16}\",\"{17}\",\"{18}\",\"{19}\"," +
                        "\"{20}\",\"{21}\",\"{22}\",\"{23}\",\"{24}\",\"{25}\",\"{26}\",\"{27}\",\"{28}\",\"{29}\"," +
                        "\"{30}\",\"{31}\"",
                        "ว/ด/ป",
                        "เวลา",
                        "ชื่อ",
                        "อายุ",
                        "เพศ",
                        "ตัวละคร",
                        "ครู",
                        "ศูนย์",
                        "SPS01",
                        "MSC01",
                        "MSC02",
                        "EMP01",
                        "MSC03",
                        "MSC04",
                        "EMP02",
                        "EMP03",
                        "SPS02",
                        "SPS03",
                        "EMP04",
                        "EMP05",
                        "MSC05",
                        "MSC06",
                        "MSC07",
                        "EMP06",
                        "EMP07",
                        "SPS04",
                        "SPS05",
                        "SPS06",
                        "ด้าน1",
                        "ด้าน2",
                        "ด้าน3",
                        "ด้าน4");
                    sw.WriteLine(data);
                }
            }
            using (StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.UTF8))
            {
                PlayerVO player = myPlayer;
                //int[] answer = this.answer;
                string gender = "";
                string charecter = "";
                switch (player.playerAvatar)
                {
                    case 0:
                        gender = "ช";
                        charecter = "M1";
                        break;
                    case 1:
                        gender = "ช";
                        charecter = "M2";
                        break;
                    case 2:
                        gender = "ญ";
                        charecter = "F1";
                        break;
                    case 3:
                        gender = "ญ";
                        charecter = "F2";
                        break;

                }
                string data = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\"," +
                        "\"{10}\",\"{11}\",\"{12}\",\"{13}\",\"{14}\",\"{15}\",\"{16}\",\"{17}\",\"{18}\",\"{19}\"," +
                        "\"{20}\",\"{21}\",\"{22}\",\"{23}\",\"{24}\",\"{25}\",\"{26}\",\"{27}\",\"{28}\",\"{29}\"," +
                        "\"{30}\",\"{31}\"",
                    System.DateTime.Now.ToString("dd/MM/yyyy"),
                    System.DateTime.Now.ToShortTimeString(),
                    player.playerName,
                    player.playerAge,
                    gender,
                    charecter,
                    player.playerTeacher,
                    player.playerCenter,
                    answer[0],
                    answer[1],
                    answer[2],
                    answer[3],
                    answer[4],
                    answer[5],
                    answer[6],
                    answer[7],
                    answer[8],
                    answer[9],
                    answer[10],
                    answer[11],
                    answer[12],
                    answer[13],
                    answer[14],
                    answer[15],
                    answer[16],
                    answer[17],
                    answer[18],
                    answer[19],
                    player.money,
                    player.work,
                    player.honor,
                    player.relationship
                    );
                sw.WriteLine(data);
            }
            myPlayer.isSave = true;
        }
    }
}
