'use strict';

exports.set_database = async function (vappOpt) {
    await this.bindDefault();
    vappOpt.bpModules.datasource.set(this, "uuuu", "123456", 2000);
    let str = await vappOpt.bpModules.datasource.getLs(this, "uuuu");
    console.log(str);
    this.body = str;
    return
}
exports.set_database.__method__ = 'POST';



// * key: ""
// * fun:""
// * project_id:""
// * url:""
// * url_id:""
// * group_id ? 这个应该不需要
exports.set_template = async function (vappOpt) {
    await this.bindDefault();
    vappOpt.bpModules.template.setTemplate({
        key: "成功返回",
        project_id: 1,
        url: "/app/landing",
        fun: "function (ctx){  }",
    });

    return
}
exports.set_template.__method__ = 'POST';

exports.save_project = async function (vappOpt) {
    await this.bindDefault();
    let Model = this.mongo('Project', { name: 'test6', is_default: 1 });
    let err = Model.save();
    this.body = "success"
    return
}
exports.save_project.__method__ = 'POST';

exports.save_url = async function (vappOpt) {
    await this.bindDefault();
    let item = { name: 'home1', path: "/a/b/c/d/e", project_id: "abcde2" };
    let Model = this.mongo('Url', item);
    // console.log(Model.save())
    let err = Model.saveByKey();
    // Model.save()
    // console.log(err)
    this.body = "success"
    return
}
exports.save_url.__method__ = 'POST';

exports.druncate_db = async function () {
    await this.bindDefault();
    let project = this.mongo("Project");
    let url = this.mongo("Url");
    project.remove({});
    url.remove({});
    return
}
exports.druncate_db.__method__ = 'GET';


exports.init_db = async function () {
    await this.bindDefault();

    let proModel = this.mongo("Project", {
        name: "默认项目",
        is_default: 1
    })
    let pro = await proModel.add();
    console.log(pro)
    await this.mongo("Url", {
        name: "路径1",
        projects: pro,
        path: "/a/b/c/d/e"
    }).saveByKey().then(function(url){
        pro.urls.push(url);
        return Promise.all([ pro.save()])
    });
    await this.mongo("Url", {
        name: "路径2",
        projects: pro,
        path: "/a1/b/c/d/e"
    }).saveByKey().then(function(url){
        pro.urls.push(url);
        return Promise.all([ pro.save()])
    });
    await this.mongo("Url", {
        name: "路径3",
        projects: pro,
        path: "/a2/b/c/d/e"
    }).saveByKey().then(function(url){
        pro.urls.push(url);
        return Promise.all([ pro.save()])
    });
    return
}
exports.init_db.__method__ = 'GET';

exports.init_db_device = async function(){
    await this.mongo("DeviceProjectRoute",{
        name: "张三的电脑",
        ipv4:"192.168.28.170",
        ipv6:""
    }).saveByKey().then(function(){

    })
}




exports.get_project_urls = async function () {
    let project = this.mongo("Project");
    this.body = await project.getProjectAndUrls();
    return
}
exports.get_project_urls.__method__ = "GET"

exports.init_db_template = async function(){
    await this.bindDefault();
    let urlModel = this.mongo("Url")
    let url = await urlModel.findOne()
    console.log(url)
    await this.mongo("TemplateUrlRoute",{
        key: "abcdef",
        is_default: 1,
        fun: "function(ctx){console.log(ctx.reqponse.ip)}",
        url: url
    }).saveByKey();
}
exports.init_db_template.__method__ = "GET"
var pathToRegExp = require('path-to-regexp');
exports.test_path = async function(){
    // compile(url);
    // .parse(url);
    let url = "/a/b/c/d/:id/fe/dfdfd"
    let s = pathToRegExp(url);
    let m = pathToRegExp.compile(url)
    let a = pathToRegExp.parse(url);
    console.log(s)
    console.log(m)
    console.log(a)
return
}
exports.test_path.__method__ = "GET"


