package service

import (
	"net/http"
	"path/filepath"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/yaoapp/yao/config"
	"github.com/yaoapp/yao/data"
	"github.com/yaoapp/yao/share"
)

// XGenFileServerV0 XGen v0.9
var XGenFileServerV0 http.Handler = http.FileServer(data.XgenV0())

// XGenFileServerV1 XGen v1.0
var XGenFileServerV1 http.Handler = http.FileServer(data.XgenV1())

// AppFileServer 应用静态文件
var AppFileServer http.Handler = http.FileServer(http.Dir(filepath.Join(config.Conf.Root, "ui")))

// Middlewares 服务中间件
var Middlewares = []gin.HandlerFunc{
	// BindDomain,
	BinStatic,
}

// BinStatic 静态文件服务
func BinStatic(c *gin.Context) {

	length := len(c.Request.URL.Path)

	if (length >= 5 && c.Request.URL.Path[0:5] == "/api/") ||
		(length >= 11 && c.Request.URL.Path[0:11] == "/websocket/") { // API & websocket
		c.Next()
		return

	} else if share.App.XGen == "1.0" && length >= 5 && c.Request.URL.Path[0:5] == "/yao/" {
		c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, "/yao")
		XGenFileServerV1.ServeHTTP(c.Writer, c.Request)
		c.Abort()
		return

	} else if share.App.XGen == "" && length >= 7 && c.Request.URL.Path[0:7] == "/xiang/" { // 数据管理后台
		c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, "/xiang")
		XGenFileServerV0.ServeHTTP(c.Writer, c.Request)
		c.Abort()
		return

	}

	// 应用内静态文件目录(/ui)
	AppFileServer.ServeHTTP(c.Writer, c.Request)
	c.Abort()
}
