import initCtx from "@/utils/ctx.js";
import {shallowRef} from "vue";
import HTTPCondURLExtension from "@/components/server/HTTPCondURLExtension.vue";
import HTTPCondURLEqIndex from "@/components/server/HTTPCondURLEqIndex.vue";
import HTTPCondURLAll from "@/components/server/HTTPCondURLAll.vue";
import HTTPCondURLPrefix from "@/components/server/HTTPCondURLPrefix.vue";
import HTTPCondURLEq from "@/components/server/HTTPCondURLEq.vue";
import HTTPCondURLRegexp from "@/components/server/HTTPCondURLRegexp.vue";
import HTTPCondURLWildcardMatch from "@/components/server/HTTPCondURLWildcardMatch.vue";
import HTTPCondUserAgentRegexp from "@/components/server/HTTPCondUserAgentRegexp.vue";
import HTTPCondParams from "@/components/server/HTTPCondParams.vue";
import HTTPCondURLNotExtension from "@/components/server/HTTPCondURLNotExtension.vue";
import HTTPCondURLNotPrefix from "@/components/server/HTTPCondURLNotPrefix.vue";
import HTTPCondURLNotEq from "@/components/server/HTTPCondURLNotEq.vue";
import HTTPCondURLNotRegexp from "@/components/server/HTTPCondURLNotRegexp.vue";
import HTTPCondUserAgentNotRegexp from "@/components/server/HTTPCondUserAgentNotRegexp.vue";
import HTTPCondMimeType from "@/components/server/HTTPCondMimeType.vue";

export default function () {
	initCtx(this)

	this.isUpdating = false
	this.cond = null
	this.paramsTitle = ""
	this.paramsCaseInsensitive = false
	this.isCaseInsensitive = false

	this.success = this.$NotifyPopup
	this.condType = (this.components.length > 0) ? this.components[0].type : ""
	this.currentComponent = this.components[0]

	this.components.forEach(c => {
		switch (c.component) {
			case "http-cond-url-extension":
				c.component = shallowRef(HTTPCondURLExtension)
				break
			case "http-cond-url-eq-index":
				c.component = shallowRef(HTTPCondURLEqIndex)
				break
			case "http-cond-url-all":
				c.component = shallowRef(HTTPCondURLAll)
				break
			case "http-cond-url-prefix":
				c.component = shallowRef(HTTPCondURLPrefix)
				break
			case "http-cond-url-eq":
				c.component = shallowRef(HTTPCondURLEq)
				break
			case "http-cond-url-regexp":
				c.component = shallowRef(HTTPCondURLRegexp)
				break
			case "http-cond-url-wildcard-match":
				c.component = shallowRef(HTTPCondURLWildcardMatch)
				break
			case "http-cond-user-agent-regexp":
				c.component = shallowRef(HTTPCondUserAgentRegexp)
				break
			case "http-cond-params":
				c.component = shallowRef(HTTPCondParams)
				break
			case "http-cond-url-not-extension":
				c.component = shallowRef(HTTPCondURLNotExtension)
				break
			case "http-cond-url-not-prefix":
				c.component = shallowRef(HTTPCondURLNotPrefix)
				break
			case "http-cond-url-not-eq":
				c.component = shallowRef(HTTPCondURLNotEq)
				break
			case "http-cond-url-not-regexp":
				c.component = shallowRef(HTTPCondURLNotRegexp)
				break
			case "http-cond-user-agent-not-regexp":
				c.component = shallowRef(HTTPCondUserAgentNotRegexp)
				break
			case "http-cond-mime-type":
				c.component = shallowRef(HTTPCondMimeType)
				break
		}
	})

	// 是否正在修改
	if (window.UPDATING_COND != null) {
		this.isUpdating = true
		this.condType = window.UPDATING_COND.type
		this.cond = window.UPDATING_COND
		if (typeof (this.cond.isCaseInsensitive) == "boolean") {
			this.isCaseInsensitive = this.cond.isCaseInsensitive
		}
	}

	this.changeCondType = function () {
		let that = this
		let c = this.components.$find(function (k, v) {
			return v.type == that.condType
		})
		this.currentComponent = c
		if (c == null || c.paramsTitle.length == 0) {
			that.paramsTitle = "条件参数"
			that.paramsCaseInsensitive = false
		} else {
			that.paramsTitle = c.paramsTitle
			if (typeof (c.caseInsensitive) != "undefined") {
				that.paramsCaseInsensitive = c.caseInsensitive
				that.$delay(function () {
					that.changeCaseInsensitive()
				})
			} else {
				that.paramsCaseInsensitive = false
			}
		}
	}

	this.$delay(function () {
		this.changeCondType()
	})

	this.changeCaseInsensitive = function () {
		let componentRef = this.$refs.component
		if (componentRef == null) {
			return
		}
		if (typeof (componentRef.changeCaseInsensitive) == "function") {
			componentRef.changeCaseInsensitive(this.isCaseInsensitive)
		}
	}
}