package com.alterdekim.game.controller;

import com.alterdekim.game.component.ReferenceLoader;
import com.alterdekim.game.component.game.AvatarInventoryType;
import com.alterdekim.game.config.ServerConfig;
import com.alterdekim.game.entity.AvatarInventory;
import com.alterdekim.game.entity.Role;
import com.alterdekim.game.entity.User;
import com.alterdekim.game.security.AuthenticationUtil;
import com.alterdekim.game.service.AvatarInventoryService;
import com.alterdekim.game.service.GoodsService;
import com.alterdekim.game.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static com.alterdekim.game.controller.FileServerController.URL_PATH;

@Slf4j
@Controller
public class StaticController {

    @Autowired
    private ServerProperties serverProperties;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private UserService userService;

    @Autowired
    private ReferenceLoader referenceLoader;

    @Autowired
    private AvatarInventoryService avatarInventoryService;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/main")
    public String mainPage(Model model) {
        User user = AuthenticationUtil.authProfile(userService).get();
        if( user.getRoles().stream().anyMatch(r -> r.getName() == Role.RoleType.RoleAdmin) ) return "redirect:/panel";
        model.addAttribute("flashVars", "game_server=http%3A%2F%2F"+serverConfig.getPublicDomain()+"%3A"+serverProperties.getPort()+"%2F&url_path_server=http%3A%2F%2F"+serverConfig.getPublicDomain()+"%3A"+serverProperties.getPort()+"%2F&portal_url=http%3A%2F%2F"+serverConfig.getPublicDomain()+"%2F&manual_server_selection=1&start_step=0");
        return "main";
    }

    @GetMapping("/")
    public String defPage(Model model) {
        if( AuthenticationUtil.authProfile(userService).isPresent() ) return "redirect:/main";
        model.addAttribute("flashVars", "game_server=http://"+serverConfig.getPublicDomain()+":"+serverProperties.getPort()+"/&amp;url_path_server=http://"+serverConfig.getPublicDomain()+":"+serverProperties.getPort()+"/&amp;is_new_user=1");
        model.addAttribute("flashVars_", "game_server=http://"+serverConfig.getPublicDomain()+":"+serverProperties.getPort()+"/&amp;url_path_server=http://"+serverConfig.getPublicDomain()+":"+serverProperties.getPort()+"/&amp;is_new_user=1");
        return "index";
    }

    @GetMapping("/panel")
    public String panel(Model model, @RequestParam(value = "section", defaultValue = "Dashboard") PanelSection section, @RequestParam(value = "table", required = false) ApiController.APITable table) {
        model.addAttribute("section", section.name());
        if( table != null ) model.addAttribute("table_add", table.name());
        model.addAttribute("script", "/" + URL_PATH + "/_js/" + section.name().toLowerCase() + ".js");
        return "panel";
    }

    @GetMapping("/panel_user")
    public String panelUser(Model model, @RequestParam(value = "id", defaultValue = "1") Integer userId) {
        model.addAttribute("user_id", userId);
        model.addAttribute("user_name", userService.getUsernameById(userId).get());
        model.addAttribute("inventory_link", "/panel_inventory?id="+userId);
        return "panel_user";
    }

    @GetMapping("/panel_inventory")
    public String panelInventory(Model model, @RequestParam(value = "id", defaultValue = "1") Integer userId) {
        model.addAttribute("user_id", userId);
        model.addAttribute("user_name", "Inventory of " + userService.getUsernameById(userId).get());
        return "panel_inventory";
    }

    @PostMapping("/panel_inventory")
    public String panelInventory(Model model, @RequestParam(value = "id", defaultValue = "1") Integer userId, @RequestParam(value = "good_id") Long goodId) {
        model.addAttribute("user_id", userId);
        model.addAttribute("user_name", "Inventory of " + userService.getUsernameById(userId).get());

        AvatarInventory av = new AvatarInventory(referenceLoader.getUserReference(userId), goodId, false, AvatarInventoryType.fromGoodId(goodsService, goodId), 0);
        avatarInventoryService.addGoodToInventory(av);

        return "redirect:/panel_inventory?id="+userId;
    }

    @GetMapping("/goods")
    public String goodsPage() {
        return "goods";
    }

    public enum PanelSection {
        Dashboard,
        Locations,
        Games,
        Preloaders,
        Users,
        Banlist,
        Cache,
        Goods,
        Items,
        Actions,
        Support,
        Add,
        Settings
    }
}
