<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Coffee.UIExtensions
{
	public class MaterialCache
	{
		public ulong hash { get; private set; }

		public int referenceCount { get; private set; }

		public Texture texture { get; private set; }

		public Material material { get; private set; }

#if UNITY_EDITOR<commit_msg>WIP: Runtime material repository<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Coffee.UIExtensions
{
	public class MaterialEntity : System.IEquatable<ulong>, System.IEquatable<MaterialEntity>
	{
		public readonly ulong id;
		public readonly Material material;
		
		public MaterialEntity(ulong id, Material material) {
			this.id = id;
			this.material = material;
		}

		public void Clear()
		{
			if(material)
			{
#if UNITY_EDITOR
				if (!Application.isPlaying)
					Object.DestroyImmediate(material, false);
				else
#endif
				Object.Destroy(material);
			}
		}

        public bool Equals(ulong other)
        {
			return this.id == other;
        }

        public bool Equals(MaterialEntity other)
        {
			return this.id == other.id;
        }
    }
	public class MaterialRepository
	{

#if UNITY_EDITOR
		[UnityEditor.InitializeOnLoadMethod]
		static void ClearCache()
		{
			foreach (var cache in materialMap.Values)
			{
				cache.Clear();
			}
			materialMap.Clear();
		}
#endif
		static MaterialEntity none = new MaterialEntity(0,null);

		static Dictionary<ulong,MaterialEntity> materialMap = new Dictionary<ulong,MaterialEntity>();
		static Dictionary<ulong,int> referenceMap = new Dictionary<ulong,int>();

		public static MaterialEntity Register(string shaderId, ulong hash, System.Action<Material> onCreateMaterial)
		{
			MaterialEntity entity;
			if(!materialMap.TryGetValue(hash, out entity))
			{
				Shader shader = Shader.Find(shaderId);
				entity = new MaterialEntity(hash, new Material(shader));

				onCreateMaterial(entity.material);
				materialMap.Add(hash, entity);
				referenceMap.Add(hash, 0);
			}

			referenceMap[hash]++;
			return entity;
		}

		public static MaterialEntity Unregister(MaterialEntity cache)
		{
			if (!none.Equals(cache) )
			{
				ulong id = cache.id;
				int count = --referenceMap[id];
				if (count <= 0)
				{
					cache.Clear();
					referenceMap.Remove(id);
					materialMap.Remove(id);
				}
			}
			return none;
		}
	}

	public class MaterialCache
	{
		public ulong hash { get; private set; }

		public int referenceCount { get; private set; }

		public Texture texture { get; private set; }

		public Material material { get; private set; }

#if UNITY_EDITOR
{'fraction_non_alphanumeric': 0.09950998869204673, 'fraction_numerical': 0.0011307953260459858, 'mean_word_length': 3.5445205479452055, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555664', 'n_tokens_mistral': 937, 'n_tokens_neox': 790, 'n_words': 208}