"use strict";

const idlUtils = require("../generated/utils");
const util = require("util");
const {Plugin} = require("../generated/Plugin");
var plugins = {
	"0": {
		"0": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"1": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"application/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"text/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"description": "Portable Document Format",
		"filename": "internal-pdf-viewer",
		"length": 2,
		"name": "PDF Viewer"
	},
	"1": {
		"0": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"1": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"application/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"text/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"description": "Portable Document Format",
		"filename": "internal-pdf-viewer",
		"length": 2,
		"name": "Chrome PDF Viewer"
	},
	"2": {
		"0": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"1": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"application/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"text/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"description": "Portable Document Format",
		"filename": "internal-pdf-viewer",
		"length": 2,
		"name": "Chromium PDF Viewer"
	},
	"3": {
		"0": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"1": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"application/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"text/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"description": "Portable Document Format",
		"filename": "internal-pdf-viewer",
		"length": 2,
		"name": "Microsoft Edge PDF Viewer"
	},
	"4": {
		"0": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"1": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"application/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "application/pdf",
			"enabledPlugin": []
		},
		"text/pdf": {
			"description": "Portable Document Format",
			"suffixes": "pdf",
			"type": "text/pdf",
			"enabledPlugin": []
		},
		"description": "Portable Document Format",
		"filename": "internal-pdf-viewer",
		"length": 2,
		"name": "WebKit built-in PDF"
	}
}
var plggs = [];
for (let i = 0; i < 5; i++) {
	plggs.push(plugins[i])

}

function randomSort(a, b) { return Math.random() > 0.5 ? -1 : 1; }
plggs.sort(randomSort);
for (let i = 0; i < 5; i++) {
	plugins[i] = idlUtils.proxy( plggs[i], 'plgs.' + i)
}

let proxy = function (tt, name) {
    return new Proxy(tt, {
        get(target, p, receiver) {
            let res = Reflect.get(target, p, receiver);
            console.log(`${name} get`, p, typeof res)

            return res;
        }
    })
}
exports.implementation = class PluginArray {
    refresh() {
    }

    get length() {
        return 5;
    }

    item() {
        console.log('getPlugin idx', arguments[0])

		let plugins = window.ENV.plugins
		var plggs = [];
		for (let i = 0; i < 5; i++) {
			plggs.push(plugins[i])

		}
		let res = plggs[arguments[0]];
		let r = {}
		r['0'] = res[0]
		r['1'] = res[1]
		r.name = res.name
		r.description = res.description
		Object.defineProperties(r, {
			name: {
				enumerable: false,
			},
			description: {
				enumerable: false,
			}
		})
        return r;
        // switch (arguments[0]){
        //   case 2:
        //     return {
        //       description:"Portable Document Format",
        //       filename:"internal-pdf-viewer",
        //       length:2,
        //       name:"Chromium PDF Viewer"
        //     }
        //   case 0:
        //     return {
        //       description:"Portable Document Format",
        //       filename:"internal-pdf-viewer",
        //       length:2,
        //       name:"PDF Viewer"
        //     }
        //   default:
        //     return {
        //       description:"Portable Document Format",
        //       filename:"internal-pdf-viewer",
        //       length:2,
        //       name:"PDF Viewer"
        //     }
        // }
        // return null;
    }

    namedItem() {
        console.log('namedItem', arguments)
        return null;
    }

    get [idlUtils.supportedPropertyIndices]() {
        return [];
    }
};
