using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InsiderAttackdetector
{
    public class DataProcessor
    {
        private static Random rnd = new Random();       

        private Dictionary<int, int> familyDict = new Dictionary<int, int>();


        public string ProcessFamilyRelationShip(int child, int parent, bool isStop)
        {
            if (isStop)
            {
                if (familyDict.TryGetValue(child, out var p))
                {
                    familyDict.Remove(child);
                }

                return "";
            }
            else
            {
                if (familyDict.TryGetValue(parent, out var p))
                {
                    familyDict.Add(child, p);

                    return $"{p},{child}";
                }
                else
                {
                    int randomizedParent;

                    lock (rnd)
                    {
                        randomizedParent = rnd.Next() + parent * 100;
                    
                        familyDict.Add(child, randomizedParent);

                        // Defensive
                        if (!familyDict.ContainsKey(parent))
                        {
                            familyDict.Add(parent, randomizedParent);
                        }
                    }

                    return $"{randomizedParent},{child}";
                }
            }
        }
    }
}
