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

namespace skyscraper5.Skyscraper.Scraper
{
    //Some of these are found at https://en.wikipedia.org/wiki/Conditional_access
    //or at https://www.dvbservices.com/identifiers/ca_system_id
    public static class CaSystemNames
    {
        private static readonly ushort[] nagravision_ids = new ushort[]
        {
            0x1800, 0x1802, 0x1803, 0x1804, 0x1805, 0x1807,
            0x1811, 0x1812, 0x1814, 0x1815,
            0x181B, 0x181D, 0x1830, 0x1831, 0x1833, 0x1835, 0x1836, 0x1837,
            0x183D, 0x183e, 0x1842, 0x1843, 0x1856, 0x1860, 0x1861,
            0x1867, 0x186a, 0x186c, 0x186d, 0x1870, 0x1871, 0x1873, 0x1874,
            0x1875, 0x1878, 0x1880, 0x1881, 0x1882, 0x1883, 0x1884, 0x1886,
            0x1887, 0x188A, 0x188D, 0x18a0, 0x18A1,
            0x1838, /* https://www.boxpirates.to/index.php?thread/136817-alles-zum-unity-media-hack-nur-noch-hier-rein-unity-media-in-nagra2-ist-wieder-o/&pageNo=20 */
            0x187E, /* https://www.digital-forum.it/showthread.php?212937-Arriva-la-Smart-Card-Satellitare-per-la-visione-gratuita-dei-canali-Rai-via-satellite&p=7337652 */
            0x1850, /* https://forums.openpli.org/topic/62690-problemen-met-lezen-van-ziggo-card-et9500-cccam/ */
            0x1868, /* https://forums.openpli.org/topic/62690-problemen-met-lezen-van-ziggo-card-et9500-cccam/ */
            0x1854, /* TSReaderLite */
            0x181f, /* TSReaderLite */
            0x181c, /* TSReaderLite */
            0x1855, /* TSReaderLite */
            0x1844, /* TSReaderLite */
            0x1840, /* TSReaderLite */
            0x18fe,
			0x1813, 0x1817, 0x1818, 0x1819, 0x1863, 0x1801, 0x1810, 0x1702, 0x1722, 0x1762,
			0x1834,
            0x1879,
            0x185f
		};


        private static ushort[] conax_ids = new ushort[]
        {
            0x0B00, 0x0B01, 0x0B02, 0x0b0f, 0x0b10, 0x0b1c, 0x0B1D,
            0x0BAA, 0x0BC1,
            0x0b77, /*according to TsReaderLite */
        };

        private static ushort[] videoguard_ids = new ushort[]
        {
			0x09f0,

			0x06F8, 0x090f, 0x091e, 0x091F, 0x092b, 0x092f,
            0x0931, 0x093e, 0x0940, 0x094C, 0x0952, 0x095E, 
            0x098C, 0x098D, 0x09AF, 0x09B2, 0x09BD, 0x09be,
            0x09C4, 0x09c7, 0x09D0, 0x09E9,
            0x098e, /* http://www.hifi-forum.de/viewthread-177-6364.html#18 */
            0x0900,
			0x0927, 0x093b, 0x0963, 0x09cd,
			0x0930, 0x0942,
			0x0911, 0x0960,
            
		};

        private static ushort[] cryptoworks_ids = new ushort[]
        {
            0x0D00, 0x0D01, 0x0D02, 0x0D03, 0x0D05, 0x0D06, 0x0D0F, 0x0D22,
            0x0D70, 0x0D95, 0x0D96, 0x0d97, 0x0D98, 0x0d07, 0x0d20
        };

        private static ushort[] irdeto_ids = new ushort[]
        {
            0x0602, 0x0603, 0x0604, 0x0606, 0x0608, 0x0609, 0x0610, 0x0614,
            0x0616, 0x0622, 0x0624, 0x0625, 0x0626, 0x0627, 0x0628, 0x0629,
            0x0630, 0x0634, 0x0640, 0x0641, 0x0647, 0x0648, 0x0650, 0x0653,
            0x0655, 0x0656, 0x0658, 0x0659, 0x0660, 0x0662, 0x0664, 0x0666,
            0x0668, 0x0690, 0x0694, 0x0695, 0x0696, 0x0697, 0x0699, 0x069A,
            0x069B, 0x069F, 0x06ad, 0x06CB, 0x06E1, 0x06E2, 0x06ED, 0x06EE,
            0x06F0, 0x06F3,
            0x0600,
            0x0646, /* TSReaderLite */
            0x06EA, /* TSReaderLite */
            0x0692,
			0x0919, 0x0961, 0x09ac,
            0x0642,
            0x0652
		};

        public static string GetHumanReadableName(ushort id)
        {
            if (id == 0x0500)
                return "Viaccess";
            
            if (id == 0x4b24)
                return "Prowill AB";

            if (id == 0x4b63)
                return "redCrypter";

            if (id == 0x0100 || id == 0x0104 || id == 0x0180 || id == 0x0186)
                return "Mediaguard";

            if (id == 0x4b64)
                return "Samsung TVKey";
            
            foreach (ushort ndsId in videoguard_ids)
                if (ndsId == id)
                    return "NDS Videoguard";

            if (id == 0x4af4)
                return "Marlin Crypt";

            foreach (ushort nagravisionId in nagravision_ids)
                if (nagravisionId == id)
                    return "Nagravision";

            foreach (ushort conaxId in conax_ids)
                if (conaxId == id)
                    return "Conax";

            foreach (ushort cryptoworksId in cryptoworks_ids)
                if (cryptoworksId == id)
                    return "Cryptoworks";

            foreach (ushort irdetoId in irdeto_ids)
                if (irdetoId == id)
                    return "Irdeto";
            
            if (id == 0x4ad0 || id == 0x4ad1 || id == 0x7be1)
                return "XCrypt";

            if (id == 0x4adc)
                return "SafeAccess";

            if (id == 0x4aea)
                return "Cryptoguard";

            if (id == 0x4AFC)
                return "Panaccess";

            if (id == 0x0007 || id == 0x4aee || id == 0x5581)
                return "Bulcrypt";

            if (id == 0x4AE0 || id == 0x4AE1)
                return "DRE-Crypt";

            if (id == 0x2200 || id == 0x223b || id == 0x22e2 || id == 0x22E3)
                return "CodiCrypt";

            if (id == 0x4A70)
                return "DreamCrypt";

            if (id == 0x4ABF)
                return "CTI-CAS";

            if (id == 0x1751)
                return "Betacrypt";

            if (id == 0x4AD4)
                return "Widevine";

            if (id == 0x4AA0 || id == 0x4AA1)
                return "KeyFly";

            if (id == 0x4A60 || id == 0x4A61 || id == 0x4A63)
                return "Neotioncrypt";  //"Skycrypt"

            if (id == 0x2600)
                return "BISS";

            if (id == 0x0e00 || id == 0x0e01)
                return "PowerVU";

            // https://wiki.zebradem.com/wiki/index.php?title=Zugangsberechtigungssystem
            if (id == 0x4800)
                return "AccessGate";

            if (id == 0x0700)
                return "Digicipher 2";

            if (id == 0x5501)
                return "Griffin";

            if (id == 0x1000)
                return "RAS";

            if (id == 0xa101)
                return "RusCrypto";

            if (id == 0x4b00)
                return "SafeView";

            if (id == 0x5500)
                return "Z-Crypt";

            if (id == 0x4abf)
                return "Wellfly";

            //from OSCAM
            if (id == 0x1010)
                return "Tandberg/Director";

            //https://en.wikipedia.org/wiki/Conditional_access
            if (id == 0x4aeb)
                return "Abel Quintic";

            if (id == 0x4af0 || id == 0x4af2 || id == 0x4b4b)
                return "ABV";

            if (id == 0x4b19)
                return "RCAS";

            if (id == 0x4b30 || id == 0x4b31)
                return "ViCAS";

            if (id == 0x4a20)
                return "AlphaCrypt";

            if (id == 0x1700 || id == 0x1701)
                return "VCAS";

            if (id >= 0x1703 && id <= 0x1721)
                return "VCAS";

            if (id >= 0x1723 && id <= 0x1761)
                return "VCAS";

            if (id >= 0x1763 && id <= 0x17ff)
                return "VCAS";

            if (id >= 0x5601 && id <= 0x5604)
                return "VCAS";

            if (id == 0x2610)
                return "BISS 2";

            if (id >= 0x27a0 && id <= 0x27a4)
                return "ICAS";

            if (id == 0x4900)
                return "China Crypt";

            if (id == 0x22f0)
                return "Codicrypt";

            if (id == 0x0b03 || id == 0x0b04 || id == 0x0b05 || id == 0x0b06 || id == 0x0b07)
                return "Conax CAS 3";

            if (id == 0x4ae4)
                return "CoreCrypt";

            if (id == 0x4347)
                return "CryptOn";

            if (id == 0x4a10)
                return "EasyCas";

            if (id == 0x2719 || id == 0xead0)
                return "InCrypt Cas";

            if (id == 0x0464)
                return "EuroDec";

            if (id == 0x5448)
                return "Gospell VisionCrypt";

            if (id == 0x4a02 || id == 0x4af6 || id == 0x4b00 || id == 0x4b01 || id == 0x4b02)
                return "Tongfang";

            if (id == 0x4ac1)
                return "Latens";

            if (id == 0x4a80)
                return "ThalesCrypt";

            if (id == 0x56d0)
                return "Onnet";

            //https://de.wikipedia.org/wiki/Zugangsberechtigungssystem
            if (id == 0x4ade)
                return "CerberCrypt";

            if (id == 0x4aef)
                return "NetUP";

            if (id == 0x1845)
	            return "Nagravision [???]";

            if (id == 0x405f)
	            return "sophia.net [Skyscraper Proprietary]";

            if (id == 21319)
                return "GkWare";

            if (id >= 0x2200 && id <= 0x22ff)
                return "Harmonic";

            if (id == 0x4b4a)
                return "Topwell";

            if (id >= 0x4ab0 && id <= 0x4abf)
                return "Compunicate";

            if (id == 0x0649)
                return "Irdeto (?)";

            if (id > 0xaa01)
                return String.Format("Invalid 0x{0:X4}", id);

            if (id == 0x09b5)
                return "NDS Videoguard (?)";

            if ((id >= 0x7700) && (id <= 0x7704))
	            return "Cifra";

            if (id == 0x2710)
	            return "DRE-Crypt";

            if (id == 0x188e)
	            return "Nagravision (?)";

            if ((id >= 0x1ec0) && (id <= 0x1ec2))
	            return "Cryptoguard";

            if ((id == 0x0652) ||(id == 0x0651))
                return "Irdeto";

			return "???";
        }
    }
}
