using UnityEngine;

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

public class Player : MonoBehaviour 
{
    #region 变量

    #region 引用变量

    private GameObject Head1;
    private GameObject Head2;
    private GameObject Head3;
    private GameObject HeadWear1;
    private GameObject HeadWear2;
    private GameObject HeadWear3;
    private SpriteRenderer EyeSr;
    private SpriteRenderer MouseSr;
    private SpriteRenderer TopSr;
    private SpriteRenderer DressSr;
    private SpriteRenderer LeftShoeSr;
    private SpriteRenderer RightShoeSr;

    #endregion

    private GameObject CurrentHead;
    private GameObject CurrentHeadWear;

    private List<Sprite> EyeList;
    private List<Sprite> MouseList;
    private List<Sprite> TopList;
    private List<Sprite> DressList;
    private List<Sprite> ShoeList;
    private List<GameObject> HeadList;
    private List<GameObject> HeadWearList;

    #endregion

    private void Awake()
    {
        #region 引用变量赋值

        Hashtable hashtable = new Hashtable();
        Transform[] transforms = GetComponentsInChildren<Transform>(true);

        for (int i = 0; i < transforms.Length; i++)
        {
            hashtable.Add(transforms[i].name, transforms[i]);
        }

        Head1 = ((Transform) hashtable["Head1"]).gameObject;
        Head2 = ((Transform)hashtable["Head2"]).gameObject;
        Head3 = ((Transform)hashtable["Head3"]).gameObject;
        HeadWear1 = ((Transform)hashtable["HeadWear1"]).gameObject;
        HeadWear2 = ((Transform)hashtable["HeadWear2"]).gameObject;
        HeadWear3 = ((Transform)hashtable["HeadWear3"]).gameObject;
        EyeSr = ((Transform)hashtable["EyeSr"]).GetComponent<SpriteRenderer>();
        MouseSr = ((Transform)hashtable["MouseSr"]).GetComponent<SpriteRenderer>();
        TopSr = ((Transform)hashtable["TopSr"]).GetComponent<SpriteRenderer>();
        DressSr = ((Transform)hashtable["DressSr"]).GetComponent<SpriteRenderer>();
        LeftShoeSr = ((Transform)hashtable["LeftShoeSr"]).GetComponent<SpriteRenderer>();
        RightShoeSr = ((Transform)hashtable["RightShoeSr"]).GetComponent<SpriteRenderer>();

        #endregion

        CurrentHead = Head1;
        CurrentHeadWear = HeadWear1;

        EyeList = new List<Sprite>();
        EyeList.Add(ManagerResource.Ins.Load<Sprite>("眼睛1", Folder.Sprite));
        EyeList.Add(ManagerResource.Ins.Load<Sprite>("眼睛2", Folder.Sprite));
        EyeList.Add(ManagerResource.Ins.Load<Sprite>("眼睛3", Folder.Sprite));

        MouseList = new List<Sprite>();
        MouseList.Add(ManagerResource.Ins.Load<Sprite>("嘴巴1", Folder.Sprite));
        MouseList.Add(ManagerResource.Ins.Load<Sprite>("嘴巴2", Folder.Sprite));
        MouseList.Add(ManagerResource.Ins.Load<Sprite>("嘴巴3", Folder.Sprite));

        TopList = new List<Sprite>();
        TopList.Add(ManagerResource.Ins.Load<Sprite>("上衣1", Folder.Sprite));
        TopList.Add(ManagerResource.Ins.Load<Sprite>("上衣2", Folder.Sprite));
        TopList.Add(ManagerResource.Ins.Load<Sprite>("上衣3", Folder.Sprite));

        DressList = new List<Sprite>();
        DressList.Add(ManagerResource.Ins.Load<Sprite>("裙子1", Folder.Sprite));
        DressList.Add(ManagerResource.Ins.Load<Sprite>("裙子2", Folder.Sprite));
        DressList.Add(ManagerResource.Ins.Load<Sprite>("裙子3", Folder.Sprite));

        ShoeList = new List<Sprite>();
        ShoeList.Add(ManagerResource.Ins.Load<Sprite>("鞋子1", Folder.Sprite));
        ShoeList.Add(ManagerResource.Ins.Load<Sprite>("鞋子2", Folder.Sprite));
        ShoeList.Add(ManagerResource.Ins.Load<Sprite>("鞋子3", Folder.Sprite));

        HeadList = new List<GameObject>();
        HeadList.Add(Head1);
        HeadList.Add(Head2);
        HeadList.Add(Head3);

        HeadWearList = new List<GameObject>();
        HeadWearList.Add(HeadWear1);
        HeadWearList.Add(HeadWear2);
        HeadWearList.Add(HeadWear3);
    }

    private void FixedUpdate()
    {

    }


    #region 换装函数

    public void NextHead()
    {
        int index = HeadList.IndexOf(CurrentHead);

        CurrentHead.SetActive(false);

        CurrentHead = HeadList[(index + 1)%HeadList.Count];
        CurrentHead.SetActive(true);
    }

    public void PreviousHead()
    {
        int index = HeadList.IndexOf(CurrentHead);

        CurrentHead.SetActive(false);

        CurrentHead = HeadList[(index - 1 + HeadList.Count) % HeadList.Count];
        CurrentHead.SetActive(true);
    }

    public void NextEye()
    {
        int index = EyeList.IndexOf(EyeSr.sprite);

        EyeSr.sprite = EyeList[(index + 1) % EyeList.Count];
    }

    public void PreviousEye()
    {
        int index = EyeList.IndexOf(EyeSr.sprite);

        EyeSr.sprite = EyeList[(index - 1 + EyeList.Count) % EyeList.Count];
    }

    public void NextMouse()
    {
        int index = MouseList.IndexOf(MouseSr.sprite);

        MouseSr.sprite = MouseList[(index + 1) % MouseList.Count];
    }

    public void PreviousMouse()
    {
        int index = MouseList.IndexOf(MouseSr.sprite);

        MouseSr.sprite = MouseList[(index - 1 + MouseList.Count) % MouseList.Count];
    }

    public void NextHeadWear()
    {
        int index = HeadWearList.IndexOf(CurrentHeadWear);

        CurrentHeadWear.SetActive(false);

        CurrentHeadWear = HeadWearList[(index + 1) % HeadWearList.Count];
        CurrentHeadWear.SetActive(true);
    }

    public void PreviousHeadWear()
    {
        int index = HeadWearList.IndexOf(CurrentHeadWear);

        CurrentHeadWear.SetActive(false);

        CurrentHeadWear = HeadWearList[(index - 1 + HeadWearList.Count)%HeadWearList.Count];
        CurrentHeadWear.SetActive(true);
    }

    public void NextTop()
    {
        int index = TopList.IndexOf(TopSr.sprite);

        TopSr.sprite = TopList[(index + 1) % TopList.Count];
    }

    public void PreviousTop()
    {
        int index = TopList.IndexOf(TopSr.sprite);

        TopSr.sprite = TopList[(index - 1 + TopList.Count) % TopList.Count];
    }

    public void NextDress()
    {
        int index = DressList.IndexOf(DressSr.sprite);

        DressSr.sprite = DressList[(index + 1) % DressList.Count];
    }

    public void PreviousDress()
    {
        int index = DressList.IndexOf(DressSr.sprite);

        DressSr.sprite = DressList[(index - 1 + DressList.Count) % DressList.Count];
    }

    public void NextShoe()
    {
        int index = ShoeList.IndexOf(LeftShoeSr.sprite);

        LeftShoeSr.sprite = ShoeList[(index + 1) % ShoeList.Count];
        RightShoeSr.sprite = ShoeList[(index + 1) % ShoeList.Count];
    }

    public void PreviousShoe()
    {
        int index = ShoeList.IndexOf(LeftShoeSr.sprite);

        LeftShoeSr.sprite = ShoeList[(index - 1 + ShoeList.Count) % ShoeList.Count];
        RightShoeSr.sprite = ShoeList[(index - 1 + ShoeList.Count) % ShoeList.Count];
    }

    #endregion
}