using System;
using System.Collections.Generic;
using UnityEngine;



public class AtomeFactory : MonoBehaviour{

    public TextAsset jsonFile;

    private static AtomeFactory instance;

    private AtomesInformation atomeInJson;

    private Dictionary<string, AtomeInformation> AtomeDictionary; 

    public AtomeFactory(TextAsset jsonFile){
        AtomeFactory.instance = this;
        AtomeDictionary = new Dictionary<string, AtomeInformation>();
        atomeInJson = JsonUtility.FromJson<AtomesInformation>(jsonFile.text);
        foreach (var atome in atomeInJson.atomes){
            AtomeDictionary.Add(atome.symbol, atome);
        }
    }

    public static AtomeFactory getInstrance(){
        if(AtomeFactory.instance == null){
            Debug.LogError("no Json file");
        }
        return AtomeFactory.instance;
    }

        public static AtomeFactory getInstrance(TextAsset jsonFile){
        if(AtomeFactory.instance == null){
            AtomeFactory.instance = new AtomeFactory(jsonFile);
        }
        return AtomeFactory.instance;
    }

    public AtomeInformation createAtome(string symbol){
        if(!AtomeDictionary.ContainsKey(symbol)){
            Debug.LogError("Atome does not existe in json");
        }
        AtomeInformation atome = AtomeDictionary[symbol];
        
        return atome;
    }

}