"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const typedi_1 = require("typedi");
const services_1 = require("./../../services");
let HostController = class HostController {
    regist(router) {
        // {
        //    name:name,
        //    description:description
        // }
        router.post('/host/create', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            const result = yield this.hostService.createHostFile(userId, ctx.request.body.name, ctx.request.body.description);
            ctx.body = {
                code: result ? 0 : 1,
                msg: result ? '' : '文件已存在',
            };
        }));
        router.get('/host/filelist', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            const hostList = yield this.hostService.getHostFileList(userId);
            ctx.body = {
                code: 0,
                list: hostList,
            };
        }));
        // /host/deletefile?name=${name}
        router.get('/host/deletefile', ctx => {
            const userId = ctx.userId;
            this.hostService.deleteHostFile(userId, ctx.query.name);
            ctx.body = {
                code: 0,
            };
        });
        // /host/togglefile?name=${name}
        router.get('/host/togglefile', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            const { name } = ctx.query;
            yield this.hostService.toggleUseHost(userId, name);
            ctx.body = {
                code: 0,
            };
        }));
        // /host/getfile?name=${name}
        router.get('/host/getfile', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            const hostFile = yield this.hostService.getHostFile(userId, ctx.query.name);
            ctx.body = {
                code: 0,
                data: hostFile,
            };
        }));
        // /host/savefile?name=${name} ,content
        router.post('/host/savefile', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            yield this.hostService.saveHostFile(userId, ctx.query.name, ctx.request.body);
            ctx.body = {
                code: 0,
            };
        }));
        router.get('/host/download', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const userId = ctx.userId;
            const name = ctx.query.name;
            const content = yield this.hostService.getHostFile(userId, name);
            ctx.set('Content-disposition', `attachment;filename=${encodeURI(name)}.json`);
            ctx.body = content;
        }));
        router.get('/host/import', (ctx) => __awaiter(this, void 0, void 0, function* () {
            const { userId, query } = ctx;
            const hostFileUrl = query.url;
            try {
                const hostFile = yield this.hostService.importRemoteHostFile(userId, hostFileUrl);
                ctx.body = {
                    code: 0,
                    data: hostFile,
                };
            }
            catch (e) {
                ctx.body = {
                    code: 1,
                    msg: e,
                };
            }
        }));
    }
};
__decorate([
    typedi_1.Inject(),
    __metadata("design:type", services_1.HostService)
], HostController.prototype, "hostService", void 0);
HostController = __decorate([
    typedi_1.Service()
], HostController);
exports.HostController = HostController;
//# sourceMappingURL=host.js.map