// Created with MonoDevelop
//
//    Copyright (C) 2018 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Linq;
using System.Runtime.Serialization;

namespace Hiperscan.AnalysisModule.Authentification
{
    [DataContract]
    [Serializable]
    public class AccessDatabaseInfo
    {
        private AccessDatabaseInfo()
        {
        }

        public AccessDatabaseInfo(string dir_name,
            AccessDatabaseType database_type, 
            AnalysisType analysis_type)
        {
            this.DirName = dir_name;
            this.DatabaseType = database_type;
            this.AnyalysisType = analysis_type;
            this.DeviceKey = null;
        }

        public AccessDatabaseInfo Clone()
        {
            AccessDatabaseInfo tmp = new AccessDatabaseInfo();
            tmp.AnyalysisType = this.AnyalysisType;
            tmp.DatabaseType = this.DatabaseType;
            tmp.DeviceKey = this.DeviceKey?.ToArray();
            tmp.DirName = this.DirName;
            tmp.HasAccess = this.HasAccess;

            return tmp;
        }

        public override string ToString()
        {
            string info = "AccessDatabaseInfo:\n";

            info += String.Format(" => AnalysisType  :  {0}\n", this.AnyalysisType);
            info += String.Format(" => DatabaseType  : {0}\n", this.DatabaseType);
            info += String.Format(" => DeviceKey     : {0}\n", this.DeviceKey == null ? "No" : "Yes");
            info += String.Format(" => ServerLocation: {0}\n", this.DirName);
            info += String.Format(" => HasAccess     : {0}\n", this.HasAccess);

            return info;    
        }

        public override int GetHashCode()
        {
            return (int)(this.DirName.GetHashCode() + this.AnyalysisType.GetHashCode());
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (this == obj)
                return true;

            AccessDatabaseInfo tmp = obj as AccessDatabaseInfo;
            if (tmp == null)
                return false;

            return this.DirName == tmp.DirName && this.AnyalysisType == tmp.AnyalysisType;
        }


        [DataMember] public string DirName { get; private set; }
        [DataMember] public AccessDatabaseType DatabaseType { get; private set; }
        [DataMember] public AnalysisType AnyalysisType { get; private set; }
        [DataMember] public byte[] DeviceKey { get; set; }
        [DataMember] public bool HasAccess { get; set; } = false;
    }
}
