package api

import (
	"fmt"
	"os"
	"path/filepath"
	"scnict/db/model"
	"scnict/db/repo"
	"time"

	"github.com/gin-gonic/gin"
)

var AssetHost = "https://oss.scnict.com"

func CreateNictRouter(router *GinRouter) {

	// dir test/aaa
	// file xxx.png
	router.POST("/upload", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
		dir := c.PostForm("dir")
		file, err := c.FormFile("file")
		if err != nil {
			return nil, err
		}
		size := fmt.Sprintf("%.2fk", float64(file.Size)/1000)
		fmt.Println(size)
		basePath := "./assets/nict/"
		assetPath := filepath.Base(file.Filename)
		if len(dir) > 0 {
			assetPath = fmt.Sprintf("%s/%s", dir, filepath.Base(file.Filename))
		}
		filename := fmt.Sprintf("%s%s", basePath, assetPath)
		err = os.MkdirAll(fmt.Sprintf("%s%s", basePath, dir), os.ModePerm)
		fmt.Println(err)
		if err := c.SaveUploadedFile(file, filename); err != nil {
			return nil, err
		}
		return fmt.Sprintf("%s/nict/%s", AssetHost, assetPath), nil
	})

	CreateCRUD(router, "/category", &CRUDOption{
		Collection: repo.CollectionCategory,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictCategory{}
			err := c.ShouldBindJSON(cat)
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictCategory{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"type", "value"},
	})

	CreateCRUD(router, "/pump", &CRUDOption{
		Collection: repo.CollectionPumps,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictPump{}
			err := c.ShouldBindJSON(cat)
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictPump{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"category", "name", "model", "desc", "thumbnail"},
		DetailProject: []string{"category", "name", "model", "desc", "thumbnail", "detail"},
	})

	CreateCRUD(router, "/parts", &CRUDOption{
		Collection: repo.CollectionParts,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictParts{}
			err := c.ShouldBindJSON(cat)
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictParts{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"category", "name", "model", "desc", "thumbnail"},
		DetailProject: []string{"category", "name", "model", "desc", "thumbnail", "detail"},
	})

	CreateCRUD(router, "/maintain", &CRUDOption{
		Collection: repo.CollectionMaintain,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictMaintain{}
			err := c.ShouldBindJSON(cat)
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictMaintain{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"category", "name", "model", "desc", "thumbnail"},
		DetailProject: []string{"category", "name", "model", "desc", "thumbnail", "detail"},
	})

	CreateCRUD(router, "/product", &CRUDOption{
		Collection: repo.CollectionProducts,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictProduct{}
			err := c.ShouldBindJSON(cat)

			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictProduct{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"type", "detail"},
	})

	CreateCRUD(router, "/news", &CRUDOption{
		Collection: repo.CollectionNews,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictNews{}
			err := c.ShouldBindJSON(cat)
			cat.CreateTime = time.Now()
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictNews{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"createTime", "title", "subtitle", "thumbnail"},
		DetailProject: []string{"createTime", "title", "subtitle", "thumbnail", "detail"},
	})

	CreateCRUD(router, "/recruit", &CRUDOption{
		Collection: repo.CollectionRecruits,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictRecruit{}
			err := c.ShouldBindJSON(cat)
			cat.CreateTime = time.Now()

			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictRecruit{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"createTime", "title", "type", "detail"},
	})

	CreateCRUD(router, "/banner", &CRUDOption{
		Collection: repo.CollectionBanners,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictBanner{}
			err := c.ShouldBindJSON(cat)
			cat.CreateTime = time.Now()
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictBanner{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"createTime", "title", "subtitle", "image"},
	})

	CreateCRUD(router, "/partner", &CRUDOption{
		Collection: repo.CollectionPartners,
		NewModel2: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
			cat := &model.NictPartner{}
			err := c.ShouldBindJSON(cat)
			cat.CreateTime = time.Now()
			if err != nil {
				return nil, err
			}
			return cat, nil
		},
		EmtyModel: func(*gin.Context) interface{} {
			return &model.NictBanner{}
		},
		SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} {
			return map[string]interface{}{}
		},
		SearchProject: []string{"createTime", "image"},
	})
}
