<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.DmzModel;
using Core.DomainModel;
using Core.DomainServices;
using Infrastructure.DataAccess;
using Infrastructure.DmzDataAccess;
using Core.DomainServices.Encryption;
using Infrastructure.DmzSync.Services.Interface;

namespace Infrastructure.DmzSync.Services.Impl
{
    public class TokenSyncService : ISyncService
    {
        private IGenericRepository<Token> _dmzTokenRepo;
        private IGenericRepository<MobileToken> _masterTokenRepo;

        public TokenSyncService(IGenericRepository<Token> dmzTokenRepo, IGenericRepository<MobileToken> masterTokenRepo)
        {
            _dmzTokenRepo = dmzTokenRepo;
            _masterTokenRepo = masterTokenRepo;
        }

        /// <summary>
        /// Syncs all MobileTokens from DMZ database to OS2 database.
        /// </summary>
        public void SyncFromDmz()
        {
            var tokens = _dmzTokenRepo.AsQueryable().ToList();
            var max = tokens.Count;

            for (var i = 0; i < max; i++)
            { 
                Console.WriteLine("Syncing token " + i + " of " + max + " from DMZ.");
                var token = tokens[i];
                token = Encryptor.DecryptToken(token);
                if (_masterTokenRepo.AsQueryable().Any(x => x.Guid.Equals(new Guid(token.GuId))))
                {
                    _masterTokenRepo.AsQueryable().First(x => x.Guid.Equals(new Guid(token.GuId))).Status = (MobileTokenStatus)token.Status;
                    _masterTokenRepo.Save();
                }
            }
        }


        // Dont run this method before syncing people.
        /// <summary>
        /// Syncs all MobileTokens from OS2 database to DMZ database.
        /// Do not run this before having synced people.
        /// </summary>
        public void SyncToDmz()
        {
            var tokens = _masterTokenRepo.AsQueryable().Where(x => x.Person.IsActive).ToList();
            var max = tokens.Count;

            for (var i = 0; i < max; i++)
            { //Encrypt token before insert
                Console.WriteLine("Syncing token " + i + " of " + max + " to DMZ.");
                var masterToken = tokens[i];
                var dmzToken = new Token
                {
                    Status = (int)masterToken.Status,
                    GuId = masterToken.Guid.ToString(),
                    TokenString = masterToken.Token,
                    ProfileId = masterToken.PersonId,
                };
                dmzToken = Encryptor.EncryptToken(dmzToken);
                _dmzTokenRepo.Insert(dmzToken);
            }
            _dmzTokenRepo.Save();
        }

        /// <summary><commit_msg>Fixed duplicate key error in TokenSync<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.DmzModel;
using Core.DomainModel;
using Core.DomainServices;
using Infrastructure.DataAccess;
using Infrastructure.DmzDataAccess;
using Core.DomainServices.Encryption;
using Infrastructure.DmzSync.Services.Interface;

namespace Infrastructure.DmzSync.Services.Impl
{
    public class TokenSyncService : ISyncService
    {
        private IGenericRepository<Token> _dmzTokenRepo;
        private IGenericRepository<MobileToken> _masterTokenRepo;

        public TokenSyncService(IGenericRepository<Token> dmzTokenRepo, IGenericRepository<MobileToken> masterTokenRepo)
        {
            _dmzTokenRepo = dmzTokenRepo;
            _masterTokenRepo = masterTokenRepo;
        }

        /// <summary>
        /// Syncs all MobileTokens from DMZ database to OS2 database.
        /// </summary>
        public void SyncFromDmz()
        {
            var tokens = _dmzTokenRepo.AsQueryable().ToList();
            var max = tokens.Count;

            for (var i = 0; i < max; i++)
            { 
                Console.WriteLine("Syncing token " + i + " of " + max + " from DMZ.");
                var token = tokens[i];
                token = Encryptor.DecryptToken(token);
                if (_masterTokenRepo.AsQueryable().Any(x => x.Guid.Equals(new Guid(token.GuId))))
                {
                    _masterTokenRepo.AsQueryable().First(x => x.Guid.Equals(new Guid(token.GuId))).Status = (MobileTokenStatus)token.Status;
                    _masterTokenRepo.Save();
                }
            }
        }


        // Dont run this method before syncing people.
        /// <summary>
        /// Syncs all MobileTokens from OS2 database to DMZ database.
        /// Do not run this before having synced people.
        /// </summary>
        public void SyncToDmz()
        {
            var tokens = _masterTokenRepo.AsQueryable().Where(x => x.Person.IsActive).ToList();
            var max = tokens.Count;

            for (var i = 0; i < max; i++)
            { //Encrypt token before insert
                Console.WriteLine("Syncing token " + i + " of " + max + " to DMZ.");
                var masterToken = tokens[i];
                var dmzToken = new Token
                {
                    Id = masterToken.Id,
                    Status = (int)masterToken.Status,
                    GuId = masterToken.Guid.ToString(),
                    TokenString = masterToken.Token,
                    ProfileId = masterToken.PersonId,
                };
                dmzToken = Encryptor.EncryptToken(dmzToken);
                _dmzTokenRepo.Insert(dmzToken);
            }
            _dmzTokenRepo.Save();
        }

        /// <summary>
{'fraction_non_alphanumeric': 0.09978655282817503, 'fraction_numerical': 0.001422981145499822, 'mean_word_length': 1.8256281407035175, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '5392418', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1504, 'n_words': 340}