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

[CreateAssetMenu(menuName = "Create new Scriptable/UserList")]
public class UserList : ScriptableObject
{
    [Header("Template")]
    // Reference to User template
    public ScriptableObject userTemplate;

    [Header("Users")]
    // List of users
    public List<User> users = new List<User>();
    // Current user
    public int currentUserIndex = 0;

    // Create a new User
    public User CreateNewUser(string name, Sprite avatar)
    {
        User user = ScriptableObject.CreateInstance<User>();
        user.username = name;
        user.avatar = avatar;
        return user;
    }

    // Create a new User and add to list
    public User CreateAndAddNewUser(string name, Sprite avatar)
    {
        User user = CreateNewUser(name, avatar);
        users.Add(user);
        EditorUtility.SetDirty(this);
        AssetDatabase.CreateAsset(user, $"Assets/Common/ScriptableObjects/Users/{name}.asset");
        AssetDatabase.SaveAssets();
        return user;
    }

    // Get user by username, returns `null` if no user can be found with such name
    public User GetUserByUsername(string username)
    {
        foreach (User user in users)
            if (user.username == username) return user;

        return null;
    }
}
