﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
/*
 * Asset trigger container임
 * */
public class AssetTriggerXmlContainer : XmlContainer {
    
    //player의 이름
    string player="";
     
    //triggerParam
    TriggerParamXmlContainer triggerParam = new TriggerParamXmlContainer();

    //asset trigger에 고정된 action list
    List<ActionXmlContainer> actionList = new List<ActionXmlContainer>();

    //player 이름
    public string PlayerName
    {
        get
        {
            return player;
        }
        set
        {
            player = value;
        }
    }
    public TriggerParamXmlContainer TriggerParameter
    {
        get
        {
            return triggerParam;
        }
        set
        {
            triggerParam = value;
        }
    }
    public List<ActionXmlContainer> ActionList
    {
        get
        {
            return actionList;
        }
        set
        {
            actionList = value;
        }
    }

    public AssetTriggerXmlContainer()
    {
        _xmlType = XmlType.ASSETTRIGGER;

    }

    public override void serialize(XmlDocument document, XmlNode parentNode)
    {
        XmlElement assetTriggerProperty = document.CreateElement(_xmlType.ToString());

        parentNode.AppendChild(assetTriggerProperty);

        assetTriggerProperty.SetAttribute("player", PlayerName);


        //trigger Param에 대한 serialize 수행하기
        triggerParam.serialize(document, assetTriggerProperty);

        //action list니까
        XmlElement actionListProperty = document.CreateElement(XmlType.ACTION.ToString() +  "List");

        assetTriggerProperty.AppendChild(actionListProperty);

        for(int i=0; i<actionList.Count; i++)
        {
            actionList[i].serialize(document, actionListProperty);
        }
    }
   
    public override void deserialize(XmlNode parentNode)
    {
        

        if (parentNode.Name == _xmlType.ToString())
        {
            ActionList.Clear();

            PlayerName = parentNode.Attributes["player"].InnerText;

            XmlNodeList childList = parentNode.ChildNodes;

            foreach (XmlNode xn in childList)
            {
                if (xn.Name == XmlType.TRIGGERPARAM.ToString())
                {
                    //trigger param deserialize하기
                    TriggerParameter = new TriggerParamXmlContainer();
                    TriggerParameter.deserialize(xn);
                }
                else if (xn.Name == XmlType.ACTION.ToString() + "List")
                {
                    foreach(XmlNode xninner in xn.ChildNodes)
                    {
                        //action list deserialize하고 집어넣기
                        ActionXmlContainer axc = new ActionXmlContainer();
                        axc.deserialize(xninner);
                        ActionList.Add(axc);
                    }
                    
                }
                
            }
            


            
        }
    }

    public override void printAll()
    {
        Debug.Log("Print start for assetTrigger");
        Debug.Log("player name: " + PlayerName);
        Debug.Log("TriggerParam: ");

        TriggerParameter.printAll();

        Debug.Log("List of Action: ");

        for (int i = 0; i < ActionList.Count; i++)
        {
            Debug.Log("Action" + i.ToString());
            ActionList[i].printAll();
        }
    }






}
