local requests = require "requests"
local cjson = require "cjson"

-- data {{{ --
local M = {
    xps = {},
    time = 0,
    interval = 10000,
    args = {
        url = "https://codestats.net/",
        headers = {
            ['Content-Type'] = 'application/json',
            ['User-Agent'] = 'code-stats-nvim/0.0.1',
            ['X-API-Token'] = "",
            Accept = '*/*'
        },
    },
    filetypes = {
        [""] = "Plain text",
        ada = "Ada",
        ansible = "Ansible",
        ansible_hosts = "Ansible",
        ansible_template = "Ansible",
        c = "C/C++",
        cs = "C#",
        clojure = "Clojure",
        cmake = "CMake",
        coffee = "CoffeeScript",
        cpp = "C/C++",
        css = "CSS",
        dart = "Dart",
        diff = "Diff",
        eelixir = "HTML (EEx)",
        elixir = "Elixir",
        elm = "Elm",
        erlang = "Erlang",
        fish = "Shell Script (fish)",
        fsharp = "F#",
        gitcommit = "Git Commit Message",
        gitconfig = "Git Config",
        gitrebase = "Git Rebase Message",
        glsl = "GLSL",
        go = "Go",
        haskell = "Haskell",
        html = "HTML",
        htmldjango = "HTML (Django)",
        java = "Java",
        javascript = "JavaScript",
        json = "JSON",
        jsp = "JSP",
        jsx = "JavaScript (JSX)",
        kotlin = "Kotlin",
        markdown = "Markdown",
        objc = "Objective-C",
        objcpp = "Objective-C++",
        ocaml = "OCaml",
        pascal = "Pascal",
        perl = "Perl",
        perl6 = "Perl 6",
        pgsql = "SQL (argsgreSQL)",
        php = "PHP",
        plsql = "PL/SQL",
        puppet = "Puppet",
        purescript = "PureScript",
        python = "Python",
        ruby = "Ruby",
        rust = "Rust",
        scala = "Scala",
        scheme = "Scheme",
        scss = "SCSS",
        sh = "Shell Script",
        sql = "SQL",
        sqloracle = "SQL (Oracle)",
        tcl = "Tcl",
        tcsh = "Shell Script (tcsh)",
        tex = "TeX",
        toml = "TOML",
        typescript = "TypeScript",
        vb = "Visual Basic",
        vbnet = "Visual Basic .NET",
        vim = "VimL",
        yaml = "YAML",
        zsh = "Shell Script (Zsh)",
        ["2html"] = "2html",
        Jenkinsfile = "Jenkinsfile",
        a2ps = "a2ps",
        a65 = "a65",
        aap = "aap",
        abap = "abap",
        abaqus = "abaqus",
        abc = "abc",
        abel = "abel",
        acedb = "acedb",
        aflex = "aflex",
        ahdl = "ahdl",
        alsaconf = "alsaconf",
        amiga = "amiga",
        aml = "aml",
        ampl = "ampl",
        ant = "ant",
        antlr = "antlr",
        apache = "apache",
        apachestyle = "apachestyle",
        apiblueprint = "apiblueprint",
        applescript = "applescript",
        aptconf = "aptconf",
        arch = "arch",
        arduino = "arduino",
        art = "art",
        asciidoc = "asciidoc",
        asm = "asm",
        asm68k = "asm68k",
        asmh8300 = "asmh8300",
        asn = "asn",
        aspperl = "aspperl",
        aspvbs = "aspvbs",
        asterisk = "asterisk",
        asteriskvm = "asteriskvm",
        atlas = "atlas",
        autohotkey = "autohotkey",
        autoit = "autoit",
        automake = "automake",
        ave = "ave",
        avra = "avra",
        awk = "awk",
        ayacc = "ayacc",
        b = "b",
        baan = "baan",
        basic = "basic",
        bc = "bc",
        bdf = "bdf",
        bib = "bib",
        bindzone = "bindzone",
        blade = "blade",
        blank = "blank",
        bst = "bst",
        btm = "btm",
        bzl = "bzl",
        bzr = "bzr",
        cabal = "cabal",
        caddyfile = "caddyfile",
        calendar = "calendar",
        catalog = "catalog",
        cdl = "cdl",
        cdrdaoconf = "cdrdaoconf",
        cdrtoc = "cdrtoc",
        cf = "cf",
        cfg = "cfg",
        ch = "ch",
        chaiscript = "chaiscript",
        change = "change",
        changelog = "changelog",
        chaskell = "chaskell",
        cheetah = "cheetah",
        chill = "chill",
        chordpro = "chordpro",
        cl = "cl",
        clean = "clean",
        clipper = "clipper",
        cmusrc = "cmusrc",
        cobol = "cobol",
        coco = "coco",
        colortest = "colortest",
        conaryrecipe = "conaryrecipe",
        conf = "conf",
        config = "config",
        context = "context",
        cql = "cql",
        crm = "crm",
        crontab = "crontab",
        cryptol = "cryptol",
        crystal = "crystal",
        csc = "csc",
        csdl = "csdl",
        csh = "csh",
        csp = "csp",
        cterm = "cterm",
        ctrlh = "ctrlh",
        cucumber = "cucumber",
        cuda = "cuda",
        cupl = "cupl",
        cuplsim = "cuplsim",
        cvs = "cvs",
        cvsrc = "cvsrc",
        cweb = "cweb",
        cynlib = "cynlib",
        cynpp = "cynpp",
        d = "d",
        datascript = "datascript",
        dcd = "dcd",
        dcl = "dcl",
        debchangelog = "debchangelog",
        debcontrol = "debcontrol",
        debsources = "debsources",
        def = "def",
        denyhosts = "denyhosts",
        desc = "desc",
        desktop = "desktop",
        dictconf = "dictconf",
        dictdconf = "dictdconf",
        dircolors = "dircolors",
        dirpager = "dirpager",
        diva = "diva",
        django = "django",
        dns = "dns",
        dnsmasq = "dnsmasq",
        docbk = "docbk",
        docbksgml = "docbksgml",
        docbkxml = "docbkxml",
        dockerfile = "dockerfile",
        dosbatch = "dosbatch",
        dosini = "dosini",
        dot = "dot",
        doxygen = "doxygen",
        dracula = "dracula",
        dsl = "dsl",
        dtd = "dtd",
        dtml = "dtml",
        dtrace = "dtrace",
        dts = "dts",
        dylan = "dylan",
        dylanintr = "dylanintr",
        dylanlid = "dylanlid",
        ecd = "ecd",
        edif = "edif",
        eiffel = "eiffel",
        elf = "elf",
        elinks = "elinks",
        elmfilt = "elmfilt",
        ["ember-script"] = "ember-script",
        emblem = "emblem",
        eruby = "eruby",
        esmtprc = "esmtprc",
        esqlc = "esqlc",
        esterel = "esterel",
        eterm = "eterm",
        euphoria3 = "euphoria3",
        euphoria4 = "euphoria4",
        eviews = "eviews",
        exim = "exim",
        expect = "expect",
        exports = "exports",
        falcon = "falcon",
        fan = "fan",
        fasm = "fasm",
        fdcc = "fdcc",
        fetchmail = "fetchmail",
        fgl = "fgl",
        flexwiki = "flexwiki",
        focexec = "focexec",
        form = "form",
        forth = "forth",
        fortran = "fortran",
        foxpro = "foxpro",
        framescript = "framescript",
        freebasic = "freebasic",
        fstab = "fstab",
        fvwm = "fvwm",
        fvwm2m4 = "fvwm2m4",
        gdb = "gdb",
        gdmo = "gdmo",
        gedcom = "gedcom",
        git = "git",
        gitolite = "gitolite",
        gitsendemail = "gitsendemail",
        gkrellmrc = "gkrellmrc",
        gmpl = "gmpl",
        gnash = "gnash",
        gnuplot = "gnuplot",
        godefstack = "godefstack",
        godoc = "godoc",
        gohtmltmpl = "gohtmltmpl",
        gotexttmpl = "gotexttmpl",
        gp = "gp",
        gpg = "gpg",
        gprof = "gprof",
        grads = "grads",
        gretl = "gretl",
        groff = "groff",
        groovy = "groovy",
        group = "group",
        grub = "grub",
        gsp = "gsp",
        gtkrc = "gtkrc",
        haml = "haml",
        hamster = "hamster",
        haste = "haste",
        hastepreproc = "hastepreproc",
        haxe = "haxe",
        hb = "hb",
        help = "help",
        hercules = "hercules",
        hex = "hex",
        hgcommit = "hgcommit",
        hitest = "hitest",
        hog = "hog",
        hostconf = "hostconf",
        hostsaccess = "hostsaccess",
        htmlcheetah = "htmlcheetah",
        htmlm4 = "htmlm4",
        htmlos = "htmlos",
        i3 = "i3",
        ia64 = "ia64",
        ibasic = "ibasic",
        icemenu = "icemenu",
        icon = "icon",
        idl = "idl",
        idlang = "idlang",
        indent = "indent",
        inform = "inform",
        initex = "initex",
        initng = "initng",
        inittab = "inittab",
        ipfilter = "ipfilter",
        ishd = "ishd",
        iss = "iss",
        ist = "ist",
        j = "j",
        jal = "jal",
        jam = "jam",
        jargon = "jargon",
        jasmine = "jasmine",
        javacc = "javacc",
        jess = "jess",
        jgraph = "jgraph",
        jinja2 = "jinja2",
        jovial = "jovial",
        jproperties = "jproperties",
        jst = "jst",
        julia = "julia",
        kconfig = "kconfig",
        kivy = "kivy",
        kix = "kix",
        kscript = "kscript",
        kwt = "kwt",
        lace = "lace",
        latextoc = "latextoc",
        latte = "latte",
        ld = "ld",
        ldapconf = "ldapconf",
        ldif = "ldif",
        less = "less",
        lex = "lex",
        lftp = "lftp",
        lhaskell = "lhaskell",
        libao = "libao",
        lifelines = "lifelines",
        lilo = "lilo",
        limits = "limits",
        liquid = "liquid",
        lisp = "lisp",
        litcoffee = "litcoffee",
        lite = "lite",
        litestep = "litestep",
        loginaccess = "loginaccess",
        logindefs = "logindefs",
        logtalk = "logtalk",
        lotos = "lotos",
        lout = "lout",
        lpc = "lpc",
        lprolog = "lprolog",
        ls = "ls",
        lscript = "lscript",
        lsl = "lsl",
        lss = "lss",
        lua = "lua",
        lynx = "lynx",
        m4 = "m4",
        mail = "mail",
        mailaliases = "mailaliases",
        mailcap = "mailcap",
        make = "make",
        mako = "mako",
        mallard = "mallard",
        man = "man",
        manconf = "manconf",
        manual = "manual",
        maple = "maple",
        masm = "masm",
        mason = "mason",
        master = "master",
        matlab = "matlab",
        maxima = "maxima",
        mel = "mel",
        messages = "messages",
        mf = "mf",
        mgl = "mgl",
        mgp = "mgp",
        mib = "mib",
        mix = "mix",
        mma = "mma",
        mmix = "mmix",
        mmp = "mmp",
        modconf = "modconf",
        model = "model",
        modsim3 = "modsim3",
        modula2 = "modula2",
        modula3 = "modula3",
        monk = "monk",
        moo = "moo",
        mp = "mp",
        mplayerconf = "mplayerconf",
        mrxvtrc = "mrxvtrc",
        msidl = "msidl",
        msmessages = "msmessages",
        msql = "msql",
        mupad = "mupad",
        murphi = "murphi",
        mush = "mush",
        mustache = "mustache",
        muttrc = "muttrc",
        mysql = "mysql",
        n1ql = "n1ql",
        named = "named",
        nanorc = "nanorc",
        nasm = "nasm",
        nastran = "nastran",
        natural = "natural",
        ncf = "ncf",
        netrc = "netrc",
        netrw = "netrw",
        nginx = "nginx",
        nim = "nim",
        ninja = "ninja",
        nix = "nix",
        nosyntax = "nosyntax",
        nqc = "nqc",
        nroff = "nroff",
        nsis = "nsis",
        obj = "obj",
        occam = "occam",
        octave = "octave",
        omnimark = "omnimark",
        opencl = "opencl",
        openroad = "openroad",
        openscad = "openscad",
        opl = "opl",
        ora = "ora",
        pamconf = "pamconf",
        papp = "papp",
        passwd = "passwd",
        pcap = "pcap",
        pccts = "pccts",
        pdf = "pdf",
        pf = "pf",
        pfmain = "pfmain",
        phtml = "phtml",
        pic = "pic",
        pike = "pike",
        pilrc = "pilrc",
        pine = "pine",
        pinfo = "pinfo",
        plaintex = "plaintex",
        plantuml = "plantuml",
        pli = "pli",
        plm = "plm",
        plp = "plp",
        po = "po",
        pod = "pod",
        argsscr = "argsscr",
        pov = "pov",
        povini = "povini",
        ppd = "ppd",
        ppwiz = "ppwiz",
        prescribe = "prescribe",
        privoxy = "privoxy",
        procmail = "procmail",
        progress = "progress",
        prolog = "prolog",
        promela = "promela",
        proto = "proto",
        protocols = "protocols",
        ps1 = "ps1",
        ps1xml = "ps1xml",
        psf = "psf",
        ptcap = "ptcap",
        pug = "pug",
        purifylog = "purifylog",
        pyrex = "pyrex",
        qf = "qf",
        qml = "qml",
        quake = "quake",
        r = "r",
        racc = "racc",
        racket = "racket",
        radiance = "radiance",
        ragel = "ragel",
        raml = "raml",
        ratpoison = "ratpoison",
        rc = "rc",
        rcs = "rcs",
        rcslog = "rcslog",
        readline = "readline",
        rebol = "rebol",
        redif = "redif",
        registry = "registry",
        remind = "remind",
        resolv = "resolv",
        reva = "reva",
        rexx = "rexx",
        rhelp = "rhelp",
        rib = "rib",
        rmd = "rmd",
        rnc = "rnc",
        rng = "rng",
        rnoweb = "rnoweb",
        robots = "robots",
        rpcgen = "rpcgen",
        rpl = "rpl",
        rrst = "rrst",
        rspec = "rspec",
        rst = "rst",
        rtf = "rtf",
        samba = "samba",
        sas = "sas",
        sass = "sass",
        sather = "sather",
        sbt = "sbt",
        scilab = "scilab",
        screen = "screen",
        sd = "sd",
        sdc = "sdc",
        sdl = "sdl",
        sed = "sed",
        sendpr = "sendpr",
        sensors = "sensors",
        services = "services",
        setserial = "setserial",
        sgml = "sgml",
        sgmldecl = "sgmldecl",
        sgmllnx = "sgmllnx",
        sicad = "sicad",
        sieve = "sieve",
        simula = "simula",
        sinda = "sinda",
        sindacmp = "sindacmp",
        sindaout = "sindaout",
        sisu = "sisu",
        skill = "skill",
        sl = "sl",
        slang = "slang",
        slice = "slice",
        slim = "slim",
        slpconf = "slpconf",
        slpreg = "slpreg",
        slpspi = "slpspi",
        slrnrc = "slrnrc",
        slrnsc = "slrnsc",
        sm = "sm",
        smarty = "smarty",
        smcl = "smcl",
        smil = "smil",
        smith = "smith",
        sml = "sml",
        snnsnet = "snnsnet",
        snnspat = "snnspat",
        snnsres = "snnsres",
        snobol4 = "snobol4",
        solidity = "solidity",
        spec = "spec",
        specman = "specman",
        spice = "spice",
        splint = "splint",
        spup = "spup",
        spyce = "spyce",
        sqlanywhere = "sqlanywhere",
        sqlforms = "sqlforms",
        sqlhana = "sqlhana",
        sqlinformix = "sqlinformix",
        sqlj = "sqlj",
        sqr = "sqr",
        squid = "squid",
        srec = "srec",
        sshconfig = "sshconfig",
        sshdconfig = "sshdconfig",
        st = "st",
        stata = "stata",
        stp = "stp",
        strace = "strace",
        stylus = "stylus",
        sudoers = "sudoers",
        svg = "svg",
        svn = "svn",
        swift = "swift",
        sxhkdrc = "sxhkdrc",
        syncolor = "syncolor",
        synload = "synload",
        syntax = "syntax",
        sysctl = "sysctl",
        systemd = "systemd",
        systemverilog = "systemverilog",
        tads = "tads",
        tags = "tags",
        tak = "tak",
        takcmp = "takcmp",
        takout = "takout",
        tap = "tap",
        tar = "tar",
        taskdata = "taskdata",
        taskedit = "taskedit",
        tasm = "tasm",
        teraterm = "teraterm",
        terminfo = "terminfo",
        terraform = "terraform",
        texinfo = "texinfo",
        texmf = "texmf",
        textile = "textile",
        tf = "tf",
        thrift = "thrift",
        tidy = "tidy",
        tilde = "tilde",
        tli = "tli",
        tmux = "tmux",
        tomdoc = "tomdoc",
        tpp = "tpp",
        trasys = "trasys",
        treetop = "treetop",
        trustees = "trustees",
        tsalt = "tsalt",
        tsscl = "tsscl",
        tssgm = "tssgm",
        tssop = "tssop",
        tt2 = "tt2",
        tt2html = "tt2html",
        tt2js = "tt2js",
        twig = "twig",
        uc = "uc",
        udevconf = "udevconf",
        udevperm = "udevperm",
        udevrules = "udevrules",
        uil = "uil",
        updatedb = "updatedb",
        upstart = "upstart",
        upstreamdat = "upstreamdat",
        upstreaminstalllog = "upstreaminstalllog",
        upstreamlog = "upstreamlog",
        upstreamrpt = "upstreamrpt",
        usserverlog = "usserverlog",
        usw2kagtlog = "usw2kagtlog",
        vala = "vala",
        valgrind = "valgrind",
        vcl = "vcl",
        velocity = "velocity",
        vera = "vera",
        verilog = "verilog",
        verilogams = "verilogams",
        vgrindefs = "vgrindefs",
        vhdl = "vhdl",
        vifm = "vifm",
        vimgo = "vimgo",
        viminfo = "viminfo",
        virata = "virata",
        vmasm = "vmasm",
        voscm = "voscm",
        vrml = "vrml",
        vroom = "vroom",
        vsejcl = "vsejcl",
        vue = "vue",
        wdiff = "wdiff",
        web = "web",
        webmacro = "webmacro",
        wget = "wget",
        whitespace = "whitespace",
        winbatch = "winbatch",
        wml = "wml",
        wsh = "wsh",
        wsml = "wsml",
        wvdial = "wvdial",
        xbl = "xbl",
        xdefaults = "xdefaults",
        xf86conf = "xf86conf",
        xhtml = "xhtml",
        xinetd = "xinetd",
        xkb = "xkb",
        xmath = "xmath",
        xml = "xml",
        xmodmap = "xmodmap",
        xpm = "xpm",
        xpm2 = "xpm2",
        xquery = "xquery",
        xs = "xs",
        xsd = "xsd",
        xsl = "xsl",
        xslt = "xslt",
        xxd = "xxd",
        yacc = "yacc",
        z8a = "z8a",
        zimbu = "zimbu",
    }
}
-- }}} data --

-- luacheck: ignore 112 113
---@diagnostic disable: undefined-global
---setup
---@param conf table
function M.setup(conf)
    M = vim.tbl_deep_extend("keep", conf, M)
end

---add xp
---@param filetype string
---@param xp integer
function M.add_xp(filetype, xp)
    local language_type = M.filetypes[filetype] or filetype
    M.xps[language_type] = (M.xps[language_type] or 0) + xp
end

---send xp
---@return table
function M.send_xp()
    if #M.xps == 0 then
        return {}
    end
    local xps = M.xps
    M.xps = {}
    local args = M.args
    args.data = cjson.encode({ xps = xps, coded_at = os.date("!%Y-%m-%dT%H:%M:%S") })
    return requests.get(args)
end

return M
-- ex: foldmethod=marker
