package routes

import (
	"net/http"

	//	"log"

	"nms_nbi/features/aaaa"
	"nms_nbi/features/cdr"
	"nms_nbi/features/cm"
	"nms_nbi/features/dbrest"
	"nms_nbi/features/file"
	"nms_nbi/features/fm"
	"nms_nbi/features/lm"
	"nms_nbi/features/mml"
	"nms_nbi/features/monitor/monitor"
	"nms_nbi/features/monitor/psnet"
	"nms_nbi/features/nbi"
	"nms_nbi/features/pm"
	"nms_nbi/features/security"
	"nms_nbi/features/sm"
	"nms_nbi/features/state"
	sysconfig "nms_nbi/features/sys_config"
	sysdictdata "nms_nbi/features/sys_dict_data"
	sysdicttype "nms_nbi/features/sys_dict_type"
	sysmenu "nms_nbi/features/sys_menu"
	sysrole "nms_nbi/features/sys_role"
	sysuser "nms_nbi/features/sys_user"
	"nms_nbi/features/trace"
	udmuser "nms_nbi/features/udm_user"
	"nms_nbi/features/ue"
	"nms_nbi/lib/midware"
	"nms_nbi/lib/services"
	"nms_nbi/src/framework/middleware/collectlogs"

	"github.com/gorilla/mux"
)

type Router struct {
	Method     string
	Pattern    string
	Handler    http.HandlerFunc
	Middleware mux.MiddlewareFunc
}

var routers []Router

func init() {
	Register("POST", security.UriOauthToken, security.LoginFromOMC, nil)
	Register("POST", security.UriOauthHandshake, security.HandshakeFromOMC, nil)
	Register("DELETE", security.UriOauthToken, security.LogoutFromOMC, nil)

	Register("POST", security.CustomUriOauthToken, security.LoginFromOMC, nil)
	Register("DELETE", security.CustomUriOauthToken, security.LogoutFromOMC, nil)
	Register("POST", security.CustomUriOauthHandshake, security.HandshakeFromOMC, nil)

	// System state
	Register("GET", state.UriSysState, state.GetStateFromNF, nil)
	Register("GET", state.UriSysState2, state.GetStateFromNF, nil)
	Register("GET", state.UriSysInfoAll, state.GetAllSysinfoFromNF, nil)
	Register("GET", state.UriSysInfoOne, state.GetOneSysinfoFromNF, nil)
	Register("GET", state.UriLicenseInfoAll, state.GetAllLicenseInfoFromNF, nil)
	Register("GET", state.UriLicenseInfoOne, state.GetOneLicenseInfoFromNF, nil)

	Register("GET", state.CustomUriSysState, state.GetStateFromNF, nil)
	Register("GET", state.CustomUriSysState2, state.GetStateFromNF, nil)
	Register("GET", state.CustomUriSysInfoAll, state.GetAllSysinfoFromNF, nil)
	Register("GET", state.CustomUriSysInfoOne, state.GetOneSysinfoFromNF, nil)
	Register("GET", state.CustomUriLicenseInfoAll, state.GetAllLicenseInfoFromNF, nil)
	Register("GET", state.CustomUriLicenseInfoOne, state.GetOneLicenseInfoFromNF, nil)

	Register("GET", sm.UriOMCLocalTime, sm.GetOMCLocalTime, nil)
	Register("GET", sm.CustomUriOMCLocalTime, sm.GetOMCLocalTime, nil)

	// database management
	Register("GET", dbrest.XormGetDataUri, dbrest.DatabaseGetData, nil)
	Register("GET", dbrest.XormSelectDataUri, dbrest.DatabaseGetData, nil)
	Register("POST", dbrest.XormInsertDataUri, dbrest.DatabaseInsertData, nil)
	Register("PUT", dbrest.XormUpdateDataUri, dbrest.DatabaseUpdateData, nil)
	Register("DELETE", dbrest.XormDeleteDataUri, dbrest.DatabaseDeleteData, nil)

	Register("GET", dbrest.CustomXormGetDataUri, dbrest.DatabaseGetData, nil)
	Register("GET", dbrest.CustomXormSelectDataUri, dbrest.DatabaseGetData, nil)
	Register("POST", dbrest.CustomXormInsertDataUri, dbrest.DatabaseInsertData, nil)
	Register("PUT", dbrest.CustomXormUpdateDataUri, dbrest.DatabaseUpdateData, nil)
	Register("DELETE", dbrest.CustomXormDeleteDataUri, dbrest.DatabaseDeleteData, nil)

	Register("GET", dbrest.XormCommonUri, dbrest.DatabaseGetData, nil)
	Register("POST", dbrest.XormCommonUri, dbrest.DatabaseInsertData, nil)
	Register("PUT", dbrest.XormCommonUri, dbrest.DatabaseUpdateData, nil)
	Register("DELETE", dbrest.XormCommonUri, dbrest.DatabaseDeleteData, nil)

	Register("GET", dbrest.XormDatabaseUri, dbrest.TaskDatabaseGetData, nil)
	Register("POST", dbrest.XormDatabaseUri, dbrest.TaskDatabaseInsertData, nil)
	Register("PUT", dbrest.XormDatabaseUri, dbrest.TaskDatabaseUpdateData, nil)
	Register("DELETE", dbrest.XormDatabaseUri, dbrest.TaskDatabaseDeleteData, nil)

	Register("GET", dbrest.CustomXormCommonUri, dbrest.DatabaseGetData, nil)
	Register("POST", dbrest.CustomXormCommonUri, dbrest.DatabaseInsertData, nil)
	Register("PUT", dbrest.CustomXormCommonUri, dbrest.DatabaseUpdateData, nil)
	Register("DELETE", dbrest.CustomXormCommonUri, dbrest.DatabaseDeleteData, nil)

	Register("GET", dbrest.XormDataRestUri, dbrest.ExtDatabaseGetData, nil)
	Register("POST", dbrest.XormDataRestUri, dbrest.ExtDatabaseInsertData, nil)
	Register("PUT", dbrest.XormDataRestUri, dbrest.ExtDatabaseUpdateData, nil)
	Register("DELETE", dbrest.XormDataRestUri, dbrest.ExtDatabaseDeleteData, nil)

	Register("GET", dbrest.XormExtDataUri, dbrest.ExtDatabaseGetData, nil)
	Register("POST", dbrest.XormExtDataUri, dbrest.ExtDatabaseInsertData, nil)
	Register("PUT", dbrest.XormExtDataUri, dbrest.ExtDatabaseUpdateData, nil)
	Register("DELETE", dbrest.XormExtDataUri, dbrest.ExtDatabaseDeleteData, nil)

	Register("GET", dbrest.CustomXormExtDataUri, dbrest.ExtDatabaseGetData, nil)
	Register("POST", dbrest.CustomXormExtDataUri, dbrest.ExtDatabaseInsertData, nil)
	Register("PUT", dbrest.CustomXormExtDataUri, dbrest.ExtDatabaseUpdateData, nil)
	Register("DELETE", dbrest.CustomXormExtDataUri, dbrest.ExtDatabaseDeleteData, nil)

	// alarm restful Register
	Register("POST", fm.UriAlarms, fm.PostAlarmFromNF, nil)
	Register("Get", fm.UriAlarms, fm.GetAlarmFromNF, nil)

	Register("POST", fm.CustomUriAlarms, fm.PostAlarmFromNF, nil)
	Register("Get", fm.CustomUriAlarms, fm.GetAlarmFromNF, nil)

	// performance restful Register
	Register("POST", pm.PerformanceUri, pm.PostKPIReportFromNF, nil)
	Register("POST", pm.MeasureTaskUri, pm.PostMeasureTaskToNF, midware.LogOperate(collectlogs.OptionNew("MeasureTask", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("PUT", pm.MeasureTaskUri, pm.PutMeasureTaskToNF, midware.LogOperate(collectlogs.OptionNew("MeasureTask", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("DELETE", pm.MeasureTaskUri, pm.DeleteMeasureTaskToNF, midware.LogOperate(collectlogs.OptionNew("MeasureTask", collectlogs.BUSINESS_TYPE_DELETE)))
	Register("PATCH", pm.MeasureTaskUri, pm.PatchMeasureTaskToNF, midware.LogOperate(collectlogs.OptionNew("MeasureTask", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("POST", pm.MeasureReportUri, pm.PostMeasureReportFromNF, midware.LogOperate(collectlogs.OptionNew("MeasureTask", collectlogs.BUSINESS_TYPE_UPDATE)))

	Register("POST", pm.MeasurementUri, pm.PostMeasurementFromNF, nil)
	Register("GET", pm.MeasurementUri, pm.GetMeasurementFromNF, nil)

	Register("POST", pm.CustomPerformanceUri, pm.PostKPIReportFromNF, nil)
	Register("POST", pm.CustomMeasureTaskUri, pm.PostMeasureTaskToNF, nil)
	Register("PUT", pm.CustomMeasureTaskUri, pm.PutMeasureTaskToNF, nil)
	Register("DELETE", pm.CustomMeasureTaskUri, pm.DeleteMeasureTaskToNF, nil)
	Register("PATCH", pm.CustomMeasureTaskUri, pm.PatchMeasureTaskToNF, nil)
	Register("POST", pm.CustomMeasureReportUri, pm.PostMeasureReportFromNF, nil)

	Register("POST", pm.CustomMeasurementUri, pm.PostMeasurementFromNF, nil)
	Register("GET", pm.CustomMeasurementUri, pm.GetMeasurementFromNF, nil)

	// parameter config management
	Register("GET", cm.ParamConfigUri, cm.GetParamConfigFromNF, nil)
	Register("POST", cm.ParamConfigUri, cm.PostParamConfigToNF, midware.LogOperate(collectlogs.OptionNew("Parameter Config", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("PUT", cm.ParamConfigUri, cm.PutParamConfigToNF, midware.LogOperate(collectlogs.OptionNew("Parameter Config", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("DELETE", cm.ParamConfigUri, cm.DeleteParamConfigToNF, midware.LogOperate(collectlogs.OptionNew("Parameter Config", collectlogs.BUSINESS_TYPE_INSERT)))

	Register("GET", cm.CustomParamConfigUri, cm.GetParamConfigFromNF, nil)
	Register("POST", cm.CustomParamConfigUri, cm.PostParamConfigToNF, nil)
	Register("PUT", cm.CustomParamConfigUri, cm.PutParamConfigToNF, nil)
	Register("DELETE", cm.CustomParamConfigUri, cm.DeleteParamConfigToNF, nil)

	// Get/Create/Modify/Delete NE info
	Register("GET", cm.UriNeInfo, cm.GetNeInfo, nil)
	Register("POST", cm.UriNeInfo, cm.PostNeInfo, midware.LogOperate(collectlogs.OptionNew("NE Info", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("PUT", cm.UriNeInfo, cm.PutNeInfo, midware.LogOperate(collectlogs.OptionNew("NE Info", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("DELETE", cm.UriNeInfo, cm.DeleteNeInfo, midware.LogOperate(collectlogs.OptionNew("NE Info", collectlogs.BUSINESS_TYPE_DELETE)))

	// Get/Create/Modify/Delete NE info
	Register("GET", cm.CustomUriNeInfo, cm.GetNeInfo, nil)
	Register("POST", cm.CustomUriNeInfo, cm.PostNeInfo, nil)
	Register("PUT", cm.CustomUriNeInfo, cm.PutNeInfo, nil)
	Register("DELETE", cm.CustomUriNeInfo, cm.DeleteNeInfo, nil)

	//ne service action handle
	Register("POST", cm.UriNeService, cm.PostNeServiceAction, nil)
	//ne service action handle
	Register("POST", cm.UriNeInstance, cm.PostNeInstanceAction, nil)
	// Post MML command to NF
	Register("POST", mml.UriMML, mml.PostMMLToNF, midware.LogMML)
	Register("POST", mml.UriMMLDiscard, mml.PostMMLToNF, nil)
	Register("POST", mml.UriOmMmlExt, mml.PostMMLToOMC, midware.LogMML)
	Register("POST", mml.CustomUriMML, mml.PostMMLToNF, midware.LogMML)
	Register("POST", mml.CustomUriOmMmlExt, mml.PostMMLToOMC, midware.LogMML)
	// post mml2 (standard upf port=5002)
	Register("POST", mml.UriMML2, mml.PostMML2ToNF, midware.LogMML)
	Register("POST", mml.CustomUriMML2, mml.PostMML2ToNF, midware.LogMML)

	// Northbound Get NRM
	Register("GET", nbi.GetNRMUri, nbi.NBIGetNRMFromNF, nil)

	Register("GET", nbi.CustomGetNRMUri, nbi.NBIGetNRMFromNF, nil)

	// Import/Export NF CM
	Register("GET", cm.NeCmUri, cm.ExportCmFromNF, nil)
	Register("POST", cm.NeCmUri, cm.ImportCmToNF, midware.LogOperate(collectlogs.OptionNew("Import NF", collectlogs.BUSINESS_TYPE_INSERT)))

	Register("GET", cm.UriNeCmFile, cm.DownloadNeBackupFile, nil)
	Register("DELETE", cm.UriNeCmFile, cm.DeleteNeBackupFile, midware.LogOperate(collectlogs.OptionNew("Delete NF Backup", collectlogs.BUSINESS_TYPE_DELETE)))

	Register("GET", cm.CustomNeCmUri, cm.ExportCmFromNF, nil)
	Register("POST", cm.CustomNeCmUri, cm.ImportCmToNF, nil)

	Register("GET", cm.CustomUriNeCmFile, cm.DownloadNeBackupFile, nil)
	Register("DELETE", cm.CustomUriNeCmFile, cm.DeleteNeBackupFile, nil)

	// Software management
	Register("GET", cm.UriSoftware, cm.DownloadSoftwareFile, nil)
	//Register("POST", cm.UriSoftware, cm.UploadSoftwareFile, nil)
	Register("POST", cm.UriSoftware, cm.UploadSoftwareMultiFile, midware.LogOperate(collectlogs.OptionNew("Software", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("DELETE", cm.UriSoftware, cm.DeleteSoftwareFile, midware.LogOperate(collectlogs.OptionNew("Software", collectlogs.BUSINESS_TYPE_DELETE)))

	Register("POST", cm.UriSoftwareNE, cm.DistributeSoftwareToNF, midware.LogOperate(collectlogs.OptionNew("Software", collectlogs.BUSINESS_TYPE_OTHER)))
	Register("PUT", cm.UriSoftwareNE, cm.ActiveSoftwareToNF, midware.LogOperate(collectlogs.OptionNew("Software", collectlogs.BUSINESS_TYPE_OTHER)))
	Register("PATCH", cm.UriSoftwareNE, cm.RollBackSoftwareToNF, midware.LogOperate(collectlogs.OptionNew("Software", collectlogs.BUSINESS_TYPE_OTHER)))

	Register("GET", cm.CustomUriSoftware, cm.DownloadSoftwareFile, nil)
	Register("POST", cm.CustomUriSoftware, cm.UploadSoftwareFile, nil)
	Register("DELETE", cm.CustomUriSoftware, cm.DeleteSoftwareFile, nil)

	Register("POST", cm.CustomUriSoftwareNE, cm.DistributeSoftwareToNF, nil)
	Register("PUT", cm.CustomUriSoftwareNE, cm.ActiveSoftwareToNF, nil)
	Register("PATCH", cm.CustomUriSoftwareNE, cm.RollBackSoftwareToNF, nil)

	// License management
	Register("POST", cm.UriLicense, cm.UploadLicenseFileData, midware.LogOperate(collectlogs.OptionNew("License", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("POST", cm.UriLicenseExt, cm.UploadLicenseFileData, midware.LogOperate(collectlogs.OptionNew("License", collectlogs.BUSINESS_TYPE_INSERT)))

	Register("POST", cm.CustomUriLicense, cm.UploadLicenseFileData, nil)
	Register("POST", cm.CustomUriLicenseExt, cm.UploadLicenseFileData, nil)

	// Trace management 跟踪任务
	Register("POST", trace.UriTraceTask, trace.PostTraceTaskToNF, midware.LogOperate(collectlogs.OptionNew("Trace Task", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("PUT", trace.UriTraceTask, trace.PutTraceTaskToNF, midware.LogOperate(collectlogs.OptionNew("Trace Task", collectlogs.BUSINESS_TYPE_UPDATE)))
	Register("DELETE", trace.UriTraceTask, trace.DeleteTraceTaskToNF, midware.LogOperate(collectlogs.OptionNew("Trace Task", collectlogs.BUSINESS_TYPE_DELETE)))
	Register("GET", trace.UriTraceDecMsg, trace.ParseRawMsg2Html, nil)

	Register("POST", trace.CustomUriTraceTask, trace.PostTraceTaskToNF, nil)
	Register("PUT", trace.CustomUriTraceTask, trace.PutTraceTaskToNF, nil)
	Register("DELETE", trace.CustomUriTraceTask, trace.DeleteTraceTaskToNF, nil)

	// 网元发送执行 pcap抓包任务
	Register("POST", trace.UriTcpdumpTask, trace.TcpdumpNeTask, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("POST", trace.CustomUriTcpdumpTask, trace.TcpdumpNeTask, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_INSERT)))
	// 网元发送执行 抓包下载pcap文件
	Register("POST", trace.UriTcpdumpPcapDownload, trace.TcpdumpPcapDownload, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_EXPORT)))
	Register("POST", trace.CustomUriTcpdumpPcapDownload, trace.TcpdumpPcapDownload, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_EXPORT)))
	// 网元发送执行UPF pcap抓包
	Register("POST", trace.UriTcpdumpNeUPFTask, trace.TcpdumpNeUPFTask, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("POST", trace.CustomUriTcpdumpNeUPFTask, trace.TcpdumpNeUPFTask, midware.LogOperate(collectlogs.OptionNew("Packet Capturing Task", collectlogs.BUSINESS_TYPE_INSERT)))

	// file management
	Register("POST", file.UriFile, file.UploadFile, midware.LogOperate(collectlogs.OptionNew("File", collectlogs.BUSINESS_TYPE_INSERT)))
	Register("GET", file.UriFile, file.DownloadFile, midware.LogOperate(collectlogs.OptionNew("File", collectlogs.BUSINESS_TYPE_OTHER)))
	Register("DELETE", file.UriFile, file.DeleteFile, midware.LogOperate(collectlogs.OptionNew("File", collectlogs.BUSINESS_TYPE_DELETE)))

	Register("POST", file.CustomUriFile, file.UploadFile, nil)
	Register("GET", file.CustomUriFile, file.DownloadFile, nil)
	Register("DELETE", file.CustomUriFile, file.DeleteFile, nil)

	// AAAA
	Register("GET", aaaa.UriAAAASSO, aaaa.GetSSOFromAAAA, nil)
	// AAAA
	Register("GET", aaaa.CustomUriAAAASSO, aaaa.GetSSOFromAAAA, nil)

	// UEInfo: SMF
	Register("GET", ue.UriUEInfo, ue.GetUEInfoFromNF, nil)
	Register("GET", ue.CustomUriUEInfo, ue.GetUEInfoFromNF, nil)

	// UEInfo: PCF
	Register("GET", ue.UriPCFUser, ue.GetUEInfoFromNF, nil)
	Register("GET", ue.CustomUriPCFUser, ue.GetUEInfoFromNF, nil)
	Register("POST", ue.UriPCFUser, ue.PostPCFUserInfo, nil)
	Register("POST", ue.CustomUriPCFUser, ue.PostPCFUserInfo, nil)
	Register("PUT", ue.UriPCFUser, ue.PutPCFUserInfo, nil)
	Register("PUT", ue.CustomUriPCFUser, ue.PutPCFUserInfo, nil)
	Register("DELETE", ue.UriPCFUser, ue.DeletePCFUserInfo, nil)
	Register("DELETE", ue.CustomUriPCFUser, ue.DeletePCFUserInfo, nil)

	// PCFUEInfo: batch add/modify/delete
	Register("POST", ue.UriPCFUserM, ue.PostPCFUserInfo, nil)
	Register("POST", ue.CustomUriPCFUserM, ue.PostPCFUserInfo, nil)
	Register("PUT", ue.UriPCFUserM, ue.PutPCFUserInfo, nil)
	Register("PUT", ue.CustomUriPCFUserM, ue.PutPCFUserInfo, nil)
	Register("DELETE", ue.UriPCFUserM, ue.DeletePCFUserInfo, nil)
	Register("DELETE", ue.CustomUriPCFUserM, ue.DeletePCFUserInfo, nil)
	//PCF User file
	Register("GET", ue.UriPCFUserFileExport, ue.GetUEInfoFileExportNF, nil)
	Register("GET", ue.CustomUriPCFUserFileExport, ue.GetUEInfoFromNF, nil)
	Register("PUT", ue.UriPCFUserFileImport, ue.PutPCFUserInfo, nil)
	Register("PUT", ue.CustomUriPCFUserFileImport, ue.PutPCFUserInfo, nil)

	// UE Number
	Register("GET", ue.UriUENum, ue.GetUENumFromNF, nil)
	Register("GET", ue.CustomUriUENum, ue.GetUENumFromNF, nil)

	// NBInfo
	Register("GET", ue.UriNBInfo, ue.GetNBInfoFromNF, nil)
	Register("GET", ue.CustomUriNBInfo, ue.GetNBInfoFromNF, nil)

	// NSSF AvailableAMFs
	Register("GET", ue.UriNSSFAvailableAMFs, ue.GetAvailableAMFsFromNSSF, nil)
	Register("GET", ue.CustomUriNSSFAvailableAMFs, ue.GetAvailableAMFsFromNSSF, nil)

	// NSSF Subscriptions
	Register("GET", ue.UriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)
	Register("GET", ue.CustomUriNSSFSubscriptions, ue.GetSubscriptionsFromNSSF, nil)

	Register("POST", cdr.UriCDREvent, cdr.PostCDREventFromNF, nil)
	Register("POST", cdr.CustomUriCDREvent, cdr.PostCDREventFromNF, nil)

	// UE event
	//Register("POST", event.UriUEEvent, event.PostUEEventFromAMF, nil)

	// 进程网络
	Register("GET", psnet.UriWs, psnet.ProcessWs, nil)
	Register("POST", psnet.UriStop, psnet.StopProcess, nil)
	Register("POST", psnet.UriPing, psnet.Ping, nil)

	// 主机CPU内存监控
	Register("POST", monitor.UriLoad, monitor.LoadMonitor, nil)
	Register("GET", monitor.UriNetOpt, monitor.Netoptions, nil)
	Register("GET", monitor.UriIPAddr, monitor.IPAddr, nil)
	Register("GET", monitor.UriIoOpt, monitor.Iooptions, nil)

	// 文件资源
	Register("GET", file.UriDiskList, file.DiskList, nil)
	Register("POST", file.UriListFiles, file.ListFiles, nil)

	// 数据库连接情况
	Register("GET", dbrest.UriDbConnection, dbrest.DbConnection, nil)
	Register("GET", dbrest.CustomUriDbConnection, dbrest.DbConnection, nil)
	Register("POST", dbrest.UriDbStop, dbrest.DbStop, nil)
	Register("POST", dbrest.CustomUriDbStop, dbrest.DbStop, nil)

	// 系统备份
	Register("POST", dbrest.UriDbBackup, dbrest.DbBackup, nil)
	Register("POST", dbrest.CustomUriDbBackup, dbrest.DbBackup, nil)
	Register("POST", dbrest.UriConfBackup, dbrest.ConfBackup, nil)
	Register("POST", dbrest.CustomUriConfBackup, dbrest.ConfBackup, nil)

	// 日志表备份
	Register("POST", lm.ExtBackupDataUri, lm.ExtDatabaseBackupData, nil)
	Register("POST", lm.CustomExtBackupDataUri, lm.ExtDatabaseBackupData, nil)

	// 系统登录
	Register("POST", security.UriLogin, security.LoginOMC, nil)
	Register("POST", security.CustomUriLogin, security.LoginOMC, nil)

	// 获取验证码
	Register("GET", security.UriCaptchaImage, security.CaptchaImage, nil)
	Register("GET", security.CustomUriCaptchaImage, security.CaptchaImage, nil)

	// 登录用户信息
	Register("GET", security.UriUserInfo, security.UserInfo, midware.Authorize(nil))
	Register("GET", security.CustomUriUserInfo, security.UserInfo, midware.Authorize(nil))

	// 登录用户路由信息
	Register("GET", security.UriRouters, security.Routers, midware.Authorize(nil))
	Register("GET", security.CustomUriRouters, security.Routers, midware.Authorize(nil))

	// 参数配置信息接口添加到路由
	for _, v := range sysconfig.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// 字典类型信息接口添加到路由
	for _, v := range sysdicttype.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// 字典类型对应的字典数据信息接口添加到路由
	for _, v := range sysdictdata.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// 菜单接口添加到路由
	for _, v := range sysmenu.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// 角色接口添加到路由
	for _, v := range sysrole.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// 用户接口添加到路由
	for _, v := range sysuser.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}

	// UDM 用户信息接口添加到路由
	for _, v := range udmuser.Routers() {
		Register(v.Method, v.Pattern, v.Handler, v.Middleware)
	}
}

// To resolv rest POST/PUT/DELETE/PATCH cross domain
func OptionsProc(w http.ResponseWriter, r *http.Request) {
	services.ResponseStatusOK204NoContent(w)
}

func NewRouter() *mux.Router {
	r := mux.NewRouter()

	// set custom handle for status 404/405
	// r.NotFoundHandler = services.CustomResponseNotFound404Handler()
	// r.MethodNotAllowedHandler = services.CustomResponseMethodNotAllowed405Handler()

	r.Use(midware.LoggerTrace)
	// r.Use(midware.Cors)
	// r.Use(midware.OptionProcess)
	// r.Use(midware.ArrowIPAddr)

	for _, router := range routers {
		rt := r.Methods(router.Method).Subrouter()
		rt.HandleFunc(router.Pattern, router.Handler)
		if router.Middleware != nil {
			rt.Use(router.Middleware)
		}

	}

	return r
}

func Register(method, pattern string, handler http.HandlerFunc, middleware mux.MiddlewareFunc) {
	routers = append(routers, Router{method, pattern, handler, middleware})
}
