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

public class FriendItem : MonoBehaviour {

    InviteFriendsMenu parentmenu;
    public Text nametext;
    public Image photo;
    public Toggle toggle;

    public void Setup(InviteFriendsMenu menu, string name, string photourl)
    {
        parentmenu = menu;
        nametext.text = name;
        StartCoroutine(getPhoto(photourl));
    }
    public void OnChecked()
    {
        parentmenu.OnChecked(this.gameObject, toggle.isOn);
    }

    public void CheckAll(bool check)
    {
        toggle.isOn = check;
    }

    IEnumerator getPhoto(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            Texture2D texture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
            www.LoadImageIntoTexture(texture);
            Rect rec = new Rect(0, 0, texture.width, texture.height);
            Sprite spriteToUse = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
            photo.sprite = spriteToUse;
        }
    }
}
