let express = require('express')
let router = express.Router()
var fs = require("fs");
const { exec } = require('child_process');

let { User, Sequelize } = require('../models')
const Errors = require('../lib/errors')

router.post("/refresh", async (req, res, next) => {
	User.findByPk(req.session.UserId).then(function(selected){
var blendFilePath = "C:/Users/studzy/Documents/Github/website/rendering/avatar.blend";
var imageSavePath = "C:/Users/studzy/Documents/Github/website/rendering/"+req.session.UserID+".png";
var pythonFilePath = "C:/Users/studzy/Documents/Github/website/python_files/"+req.session.UserID+".py";
var faceFilePath = "C:/Users/studzy/Documents/Github/website/rendering/faces/defaultFace.png";
var imports = "import bpy";
var functions = "def hex_to_rgb(value):\n    gamma = 2.05\n    value = value.lstrip('#')\n    lv = len(value)\n    fin = list(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))\n    r = pow(fin[0] / 255, gamma)\n    g = pow(fin[1] / 255, gamma)\n    b = pow(fin[2] / 255, gamma)\n    fin.clear()\n    fin.append(r)\n    fin.append(g)\n    fin.append(b)\n    return tuple(fin)";
var blenderImport = "bpy.ops.wm.open_mainfile(filepath='"+blendFilePath+"')";
var	headColor = "bpy.data.objects['Head'].select = True\nbpy.data.materials['Head'].diffuse_color = hex_to_rgb('#"+User.headColor+"')\nbpy.data.materials['Face'].diffuse_color = hex_to_rgb('#"+User.headColor+"')";
var leftArmColor = "bpy.data.objects['Left Arm'].select = True\nbpy.data.objects['Left Arm'].active_material.diffuse_color = hex_to_rgb('#"+User.leftArmColor+"')";
var rightArmColor = "bpy.data.objects['Right Arm'].select = True\nbpy.data.objects['Right Arm'].active_material.diffuse_color = hex_to_rgb('#"+User.rightArmColor+"')";
var	torsoColor = "bpy.data.objects['Torso'].select = True\nbpy.data.objects['Torso'].active_material.diffuse_color = hex_to_rgb('#"+User.torsoColor+"')";
var leftLegColor = "bpy.data.objects['Left Leg'].select = True\nbpy.data.objects['Left Leg'].active_material.diffuse_color = hex_to_rgb('#"+User.leftLegColor+"')";
var rightLegColor = "bpy.data.objects['Right Leg'].select = True\nbpy.data.objects['Right Leg'].active_material.diffuse_color = hex_to_rgb('#"+User.rightLegColor+"')";
var colors = headColor+"\n"+leftArmColor+"\n"+torsoColor+"\n"+rightArmColor+"\n"+leftLegColor+"\n"+rightLegColor;
var face = "face_Image = bpy.data.images.load(filepath = '"+faceFilePath+"')\nbpy.data.textures['Face'].image = face_Image";
var render = "scene = bpy.context.scene\nscene.render.image_settings.file_format = 'PNG'\nscene.render.filepath = '"+imageSavePath+"'\nbpy.ops.render.render(write_still = 1)";
var python = imports+"\n"+functions+"\n"+blenderImport+"\n"+colors+"\n"+face+"\n"+render;

fs.writeFile("../python_files/"+req.session.UserId+".py", python, function(err,data){
    if(err) { return console.log(err) }
    console.log(data);
})

exec("blender -b -P C:/Users/studzy/Documents/Github/website/python_files/"+req.session.UserId+".py", (err, stdout, stderr) => {
    if(err) { throw err; }
    console.log("stdout: " + stdout);
    console.log("stderr: " + stderr);
});
	});
});