/*
 * @description: 路由配置
 * @Author: CP
 * @Date: 2020-08-23 21:32:54
 * @FilePath: \construction_management\web\routes\routes.go
 */
package routes

import (
	"github.com/kataras/iris/v12/mvc"
	"go.mod/bootstrap"
	"go.mod/services"
	"go.mod/web/api"
	"go.mod/web/controllers"
	"go.mod/web/middleware"
)

func Configure(b *bootstrap.Bootstrapper) {
	//service加载
	ProjectAccountService := services.NewProjectAccountService()
	LoginService := services.NewLoginService()
	ProjectService := services.NewProjectService()
	TreeService := services.NewTreeService()
	BidsectionService := services.NewBidsectionService()
	BidAccountService := services.NewBidAccountService()
	ContractService := services.NewContractService()
	// RpcService := services.NewRpcService()
	SafeService := services.NewSafeService()
	RuleService := services.NewRuleService()
	AnnexService := services.NewAnnexService()
	SafeAuditService := services.NewSafeAuditService()
	//CSRF相关
	b.Use(middleware.SetCsrf)

	//b.Party("/", protect)
	//protect := NewCsrf()

	//登录相关
	//login := mvc.New(b.Party("/", protect))
	login := mvc.New(b.Party("/"))
	login.Register(ProjectAccountService)
	login.Register(LoginService)
	login.Handle(new(controllers.LoginController))

	// todolist相关
	todo := mvc.New(b.Party("/todolist"))
	todo.Router.Use(middleware.SessionsAuth)
	todo.Handle(new(controllers.TodoController))

	// // 合同管理
	// contract := mvc.New(b.Party("/contract"))
	// contract.Router.Use(middleware.SessionsAuth)
	// contract.Handle(new(controllers.ContractController))
	// 安全巡检

	//用户相关
	account := mvc.New(b.Party("/account"))
	account.Register(ProjectAccountService)
	account.Router.Use(middleware.SessionsAuth)
	account.Handle(new(controllers.AccountController))

	// 项目相关
	project := mvc.New(b.Party("/project"))
	project.Router.Use(middleware.SessionsAuth)
	project.Register(TreeService)
	project.Register(ProjectService)
	project.Handle(new(controllers.ProjectController))

	//标段相关
	bidsection := mvc.New(b.Party("/bidsection"))
	bidsection.Register(ProjectService)
	bidsection.Router.Use(middleware.SessionsAuth)
	bidsection.Handle(new(controllers.BidsectionController))

	// apiTree.Router.Use(middleware.JwtAuth().Serve)
	// 接口相关
	// 登陆接口
	apiLogin := mvc.New(b.Party("/api/login"))
	apiLogin.Register(ProjectAccountService)
	apiLogin.Register(LoginService)
	apiLogin.Register(ProjectService)
	apiLogin.Handle(new(api.LoginApi))

	// 项目相关接口
	apiProject := mvc.New(b.Party("/api/project"))
	apiProject.Register(ProjectService)
	apiProject.Router.Use(middleware.SessionsAuth)
	apiProject.Router.Use(middleware.AccessAuth)
	apiProject.Handle(new(api.ProjectApi))

	// 项目账号相关接口
	apiProjectAccount := mvc.New(b.Party("/api/projectAccount"))
	apiProjectAccount.Register(ProjectAccountService)
	apiProjectAccount.Router.Use(middleware.SessionsAuth)
	apiProjectAccount.Router.Use(middleware.AccessAuth)
	apiProjectAccount.Handle(new(api.ProjectAccountApi))

	// 标段相关接口
	apiBidsection := mvc.New(b.Party("/api/bidsection"))
	apiBidsection.Register(BidsectionService)
	apiBidsection.Router.Use(middleware.SessionsAuth)
	apiBidsection.Router.Use(middleware.AccessAuth)
	apiBidsection.Handle(new(api.BidsectionApi))

	// 项目设置接口
	apiSetting := mvc.New(b.Party("/api/projectSetting"))
	apiSetting.Register(ProjectAccountService)
	apiSetting.Register(ProjectService)
	apiSetting.Register(BidAccountService)
	apiSetting.Router.Use(middleware.SessionsAuth)
	apiSetting.Router.Use(middleware.AccessAuth)
	apiSetting.Handle(new(api.ProjectSettingApi))

	// TreeNode相关接口
	apiTree := mvc.New(b.Party("/api/tree"))
	apiTree.Register(TreeService)
	apiTree.Router.Use(middleware.SessionsAuth)
	apiTree.Router.Use(middleware.AccessAuth)
	apiTree.Handle(new(api.TreeApi))

	// 合同管理
	apiContract := mvc.New(b.Party("/api/contract"))
	apiContract.Register(TreeService)
	apiContract.Register(ContractService)
	// 中间件
	apiContract.Router.Use(middleware.SessionsAuth)
	apiContract.Router.Use(middleware.AccessAuth)
	apiContract.Handle(new(api.ContractApi))

	// oss相关
	apiOss := mvc.New(b.Party("/api/oss"))
	apiOss.Router.Use(middleware.SessionsAuth)
	apiOss.Router.Use(middleware.AccessAuth)
	apiOss.Handle(new(api.OssApi))

	// rpc相关
	// rpc := mvc.New(b.Party("/api/rpc/test"))
	// rpc.Register(RpcService)
	// rpc.Router.Use(middleware.SessionsAuth)
	// rpc.Router.Use(middleware.AccessAuth)
	// rpc.Handle(new(api.RpcApi))

	// safe
	apiSafe := mvc.New(b.Party("/api/safe"))
	apiSafe.Register(SafeService)
	apiSafe.Router.Use(middleware.SessionsAuth)
	apiSafe.Router.Use(middleware.AccessAuth)
	apiSafe.Handle(new(api.SafeApi))

	apiSafeAudit := mvc.New(b.Party("/api/safe_audit"))
	apiSafeAudit.Register(SafeAuditService)
	apiSafeAudit.Router.Use(middleware.SessionsAuth)
	apiSafeAudit.Router.Use(middleware.AccessAuth)
	apiSafeAudit.Handle(new(api.SafeAuditApi))
	// rule
	apiRule := mvc.New(b.Party("/api/rule"))
	apiRule.Register(RuleService)
	apiRule.Router.Use(middleware.SessionsAuth)
	apiRule.Router.Use(middleware.AccessAuth)
	apiRule.Handle(new(api.RuleApi))

	// file
	apiAnnex := mvc.New(b.Party("/api/file"))
	apiAnnex.Register(AnnexService)
	apiAnnex.Router.Use(middleware.SessionsAuth)
	apiAnnex.Router.Use(middleware.AccessAuth)
	apiAnnex.Handle(new(api.AnnexApi))
}
