﻿/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, December 2020
*/

using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ItSeez3D.AvatarSdkSamples.Cloud
{
    /// <summary>
    /// PhotonAvatarNotifier should be used with avatar prefab. Raises event when prefab instance is created by Photon.
    /// </summary>
    public class PhotonAvatarNotifier : MonoBehaviour, IPunInstantiateMagicCallback
    {
        public string AvatarCode { get; set; }
        public int SpawnIdx { get; set; }
        public string HairId { get; set; }
        public string OutfitId { get; set; }

        public static event AvatarPrefabInstantiationDelegate AvatarPrefabInstantiatedEvent;
        public delegate void AvatarPrefabInstantiationDelegate(Player player, GameObject gameObject, string avatarCode, string outfitId, string hairId, int spawnIdx);
        void Start()
        {
            //var photonView = GetComponent<PhotonView>();
            //var player = photonView.Owner;
            //var instanceGameObject = gameObject;
            //AvatarPrefabInstantiatedEvent?.Invoke(player, gameObject);
        }

        public void OnPhotonInstantiate(PhotonMessageInfo info) 
        {
            var photonView = GetComponent<PhotonView>();
            var player = photonView.Owner;
            var data = info.photonView.InstantiationData;
            if (data != null && data.Length == 4)
            {
                AvatarCode = (string)data[0];
                SpawnIdx = (int)data[1];
                HairId = (string)data[2];
                OutfitId = (string)data[3];
                AvatarPrefabInstantiatedEvent?.Invoke(player, gameObject, AvatarCode, OutfitId, HairId, SpawnIdx);

            }

        }
    }
}