package back

import (
	"html/template"
	"strconv"
)

// Для быстрой вставки меню в шаблон HTML
var templateMenu template.HTML
var templateMenuTop template.HTML

// Для быстрой вставки меню в шаблон HTML (включая неактивные пункты)
var templateMenuAll template.HTML
var templateMenuAllTop template.HTML

// Для быстрой выдачи меню на пост запрос
var buildedMenu []menu
var buildedMenuTop []menu

// Для быстрой выдачи меню на пост запрос (включая неактивные пункты)
var buildedMenuAll []menu
var buildedMenuAllTop []menu

func prepareMenu() {
	m := menu{}
	mAll := menu{}

	m.Categoty = "__main"
	e := m.Select()
	if e != nil {
		logger.Println(e)
		return
	}

	mAll.Categoty = "__main"
	e = mAll.SelectAll()
	if e != nil {
		logger.Println(e)
		return
	}

	buildedMenu = make([]menu, 0)

	templateMenu = template.HTML("")

	for i := 0; i < len(m.Rows); i++ {
		if !m.Rows[i].Parent.Valid {
			buildMenu(m.Rows, &buildedMenu, m.Rows[i])
		}
	}

	buildedMenuAll = make([]menu, 0)

	templateMenuAll = template.HTML("")

	for i := 0; i < len(mAll.Rows); i++ {
		if !mAll.Rows[i].Parent.Valid {
			buildMenuAll(mAll.Rows, &buildedMenuAll, mAll.Rows[i])
		}
	}

	// TOP MENU BUILD

	m = menu{}
	mAll = menu{}

	m.Categoty = "__top"
	e = m.Select()
	if e != nil {
		logger.Println(e)
		return
	}

	mAll.Categoty = "__top"
	e = mAll.SelectAll()
	if e != nil {
		logger.Println(e)
		return
	}

	buildedMenuTop = make([]menu, 0)

	templateMenuTop = template.HTML("")

	for i := 0; i < len(m.Rows); i++ {
		if !m.Rows[i].Parent.Valid {
			buildMenuTop(m.Rows, &buildedMenuTop, m.Rows[i])
		}
	}

	buildedMenuAllTop = make([]menu, 0)

	templateMenuAllTop = template.HTML("")

	for i := 0; i < len(mAll.Rows); i++ {
		if !mAll.Rows[i].Parent.Valid {
			buildMenuAllTop(mAll.Rows, &buildedMenuAllTop, mAll.Rows[i])
		}
	}
}

func buildMenu(root []menu, out *[]menu, current menu) {

	if !current.Parent.Valid {

		if current.Link.Valid {
			// if strings.EqualFold(current.Link.String[:4], "http") {
			templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(current.ID)) + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			// } else {
			// 	templateMenu += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// }
		} else {
			templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(current.ID)) + `">` + current.Caption + `</a>`)
		}

		*out = append(*out, current)

		openSub := false

		for _, r := range root {
			if r.Parent.Valid {

				if r.Parent.Int32 == current.ID {

					if !openSub {
						templateMenu += template.HTML(`<div class="submenu">`)
						openSub = true
					}

					if r.Link.Valid {
						// if strings.EqualFold(r.Link.String[:4], "http") {
						templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						// } else {
						// templateMenu += template.HTML(`<div class="item"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// }
					} else {
						templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `">` + r.Caption + `</a>`)
					}

					(*out)[len(*out)-1].Rows = append((*out)[len(*out)-1].Rows, r)

					buildMenu(root[1:], &((*out)[len(*out)-1].Rows[len((*out)[len(*out)-1].Rows)-1]).Rows, r)

					templateMenu += template.HTML(`</div>`)
				}
			}
		}
		if openSub {
			templateMenu += template.HTML(`</div>`)
		}

		templateMenu += template.HTML(`</div>`)

	} else {

		openSub := false

		for _, r := range root {
			if r.Parent.Int32 == current.ID {

				if !openSub {
					templateMenu += template.HTML(`<div class="submenu">`)
					openSub = true
				}

				if r.Link.Valid {
					// if strings.EqualFold(r.Link.String[:4], "http") {
					templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					// } else {
					// 	templateMenu += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// }
				} else {
					templateMenu += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `">` + r.Caption + `</a>`)
				}

				*out = append(*out, r)

				// Смотрим детей текущего элемента
				buildMenu(root, &(*out)[len(*out)-1].Rows, r)

				templateMenu += template.HTML(`</div>`)
			}
		}

		if openSub {
			templateMenu += template.HTML(`</div>`)
		}
	}
}
func buildMenuTop(root []menu, out *[]menu, current menu) {

	if !current.Parent.Valid {

		if current.Link.Valid {
			// if strings.EqualFold(current.Link.String[:4], "http") {
			templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(current.ID)) + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			// } else {
			// templateMenuTop += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// }
		} else {
			templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(current.ID)) + `">` + current.Caption + `</a>`)
		}

		*out = append(*out, current)

		openSub := false

		for _, r := range root {
			if r.Parent.Valid {

				if r.Parent.Int32 == current.ID {

					if !openSub {
						templateMenuTop += template.HTML(`<div class="submenu">`)
						openSub = true
					}

					if r.Link.Valid {
						// if strings.EqualFold(r.Link.String[:4], "http") {
						templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						// } else {
						// 	templateMenuTop += template.HTML(`<div class="item"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// }
					} else {
						templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `">` + r.Caption + `</a>`)
					}

					(*out)[len(*out)-1].Rows = append((*out)[len(*out)-1].Rows, r)

					buildMenu(root[1:], &((*out)[len(*out)-1].Rows[len((*out)[len(*out)-1].Rows)-1]).Rows, r)

					templateMenuTop += template.HTML(`</div>`)
				}
			}
		}
		if openSub {
			templateMenuTop += template.HTML(`</div>`)
		}

		templateMenuTop += template.HTML(`</div>`)

	} else {

		openSub := false

		for _, r := range root {
			if r.Parent.Int32 == current.ID {

				if !openSub {
					templateMenuTop += template.HTML(`<div class="submenu">`)
					openSub = true
				}

				if r.Link.Valid {
					// if strings.EqualFold(r.Link.String[:4], "http") {
					templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					// } else {
					// 	templateMenuTop += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// }
				} else {
					templateMenuTop += template.HTML(`<div class="item"><a class="text" data-id="` + strconv.Itoa(int(r.ID)) + `">` + r.Caption + `</a>`)
				}

				*out = append(*out, r)

				// Смотрим детей текущего элемента
				buildMenu(root, &(*out)[len(*out)-1].Rows, r)

				templateMenuTop += template.HTML(`</div>`)
			}
		}

		if openSub {
			templateMenuTop += template.HTML(`</div>`)
		}
	}
}

func buildMenuAll(root []menu, out *[]menu, current menu) {

	if !current.Parent.Valid {

		idStr := strconv.Itoa(int(current.ID))

		if current.Link.Valid {
			// if strings.EqualFold(current.Link.String[:4], "http") {
			if current.Active {
				templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			} else {
				templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			}
			// } else {
			// 	if current.Active {
			// 		templateMenuAll += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// 	} else {
			// 		templateMenuAll += template.HTML(`<div class="item hide"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// 	}
			// }
		} else {
			if current.Active {
				templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `">` + current.Caption + `</a>`)
			} else {
				templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `">` + current.Caption + `</a>`)
			}
		}

		*out = append(*out, current)

		openSub := false

		for _, r := range root {
			if r.Parent.Valid {

				if r.Parent.Int32 == current.ID {

					if !openSub {
						templateMenuAll += template.HTML(`<div class="submenu">`)
						openSub = true
					}

					idStr = strconv.Itoa(int(r.ID))

					if r.Link.Valid {
						// if strings.EqualFold(r.Link.String[:4], "http") {
						if r.Active {
							templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						} else {
							templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						}
						// } else {
						// 	if r.Active {
						// 		templateMenuAll += template.HTML(`<div class="item"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// 	} else {
						// 		templateMenuAll += template.HTML(`<div class="item hide"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// 	}
						// }
					} else {
						if r.Active {
							templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
						} else {
							templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
						}
					}

					(*out)[len(*out)-1].Rows = append((*out)[len(*out)-1].Rows, r)

					buildMenuAll(root[1:], &((*out)[len(*out)-1].Rows[len((*out)[len(*out)-1].Rows)-1]).Rows, r)

					templateMenuAll += template.HTML(`</div>`)
				}
			}
		}
		if openSub {
			templateMenuAll += template.HTML(`</div>`)
		}

		templateMenuAll += template.HTML(`</div>`)

	} else {

		openSub := false

		for _, r := range root {
			if r.Parent.Int32 == current.ID {

				if !openSub {
					templateMenuAll += template.HTML(`<div class="submenu">`)
					openSub = true
				}

				idStr := strconv.Itoa(int(r.ID))

				if r.Link.Valid {
					// if strings.EqualFold(r.Link.String[:4], "http") {
					if r.Active {
						templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					} else {
						templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					}
					// } else {
					// 	if r.Active {
					// 		templateMenuAll += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// 	} else {
					// 		templateMenuAll += template.HTML(`<div class="item hide"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// 	}
					// }
				} else {
					if r.Active {
						templateMenuAll += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
					} else {
						templateMenuAll += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
					}
				}

				*out = append(*out, r)

				// Смотрим детей текущего элемента
				buildMenuAll(root, &(*out)[len(*out)-1].Rows, r)

				templateMenuAll += template.HTML(`</div>`)
			}
		}

		if openSub {
			templateMenuAll += template.HTML(`</div>`)
		}
	}
}
func buildMenuAllTop(root []menu, out *[]menu, current menu) {

	if !current.Parent.Valid {

		strID := strconv.Itoa(int(current.ID))

		if current.Link.Valid {
			// if strings.EqualFold(current.Link.String[:4], "http") {
			if current.Active {
				templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + strID + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			} else {
				templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + strID + `" href="` + current.Link.String + `">` + current.Caption + `</a>`)
			}
			// } else {
			// 	if current.Active {
			// 		templateMenuAllTop += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// 	} else {
			// 		templateMenuAllTop += template.HTML(`<div class="item hide"><div class="text" onclick="LoadMaterial('` + current.Link.String + `')" data-id="` + strconv.Itoa(int(current.ID)) + `" data-link="` + current.Link.String + `">` + current.Caption + `</div>`)
			// 	}
			// }
		} else {
			if current.Active {
				templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + strID + `">` + current.Caption + `</a>`)
			} else {
				templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + strID + `">` + current.Caption + `</a>`)
			}
		}

		*out = append(*out, current)

		openSub := false

		for _, r := range root {
			if r.Parent.Valid {

				if r.Parent.Int32 == current.ID {

					if !openSub {
						templateMenuAllTop += template.HTML(`<div class="submenu">`)
						openSub = true
					}

					idStr := strconv.Itoa(int(r.ID))

					if r.Link.Valid {
						// if strings.EqualFold(r.Link.String[:4], "http") {
						if r.Active {
							templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						} else {
							templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
						}
						// } else {
						// 	if r.Active {
						// 		templateMenuAllTop += template.HTML(`<div class="item"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// 	} else {
						// 		templateMenuAllTop += template.HTML(`<div class="item hide"><div onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
						// 	}
						// }
					} else {
						if r.Active {
							templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
						} else {
							templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + idStr + `">` + r.Caption + `</a>`)
						}
					}

					(*out)[len(*out)-1].Rows = append((*out)[len(*out)-1].Rows, r)

					buildMenuAll(root[1:], &((*out)[len(*out)-1].Rows[len((*out)[len(*out)-1].Rows)-1]).Rows, r)

					templateMenuAllTop += template.HTML(`</div>`)
				}
			}
		}
		if openSub {
			templateMenuAllTop += template.HTML(`</div>`)
		}

		templateMenuAllTop += template.HTML(`</div>`)

	} else {

		openSub := false

		for _, r := range root {
			if r.Parent.Int32 == current.ID {

				if !openSub {
					templateMenuAllTop += template.HTML(`<div class="submenu">`)
					openSub = true
				}

				strID := strconv.Itoa(int(r.ID))

				if r.Link.Valid {
					// if strings.EqualFold(r.Link.String[:4], "http") {
					if r.Active {
						templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + strID + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					} else {
						templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + strID + `" href="` + r.Link.String + `">` + r.Caption + `</a>`)
					}
					// } else {
					// 	if r.Active {
					// 		templateMenuAllTop += template.HTML(`<div class="item"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// 	} else {
					// 		templateMenuAllTop += template.HTML(`<div class="item hide"><div class="text" onclick="LoadMaterial('` + r.Link.String + `')" data-id="` + strconv.Itoa(int(r.ID)) + `" data-link="` + r.Link.String + `">` + r.Caption + `</div>`)
					// 	}
					// }
				} else {
					if r.Active {
						templateMenuAllTop += template.HTML(`<div class="item"><a class="text" data-id="` + strID + `">` + r.Caption + `</a>`)
					} else {
						templateMenuAllTop += template.HTML(`<div class="item hide"><a class="text" data-id="` + strID + `">` + r.Caption + `</a>`)
					}
				}

				*out = append(*out, r)

				// Смотрим детей текущего элемента
				buildMenuAll(root, &(*out)[len(*out)-1].Rows, r)

				templateMenuAllTop += template.HTML(`</div>`)
			}
		}

		if openSub {
			templateMenuAllTop += template.HTML(`</div>`)
		}
	}
}

// Для быстрой вставки новостей в шалбон HTML главной страницы
var templateNews template.HTML

func prepareNews() {
	news := material{}
	news.Category = "новость"
	e := news.Select(0, 8)
	if e != nil {
		logger.Println(e.Error())
		return
	}

	templateNews = template.HTML("")

	// 0 3 4 с изображениями
	for i, m := range news.Rows {

		images := `<div class="image no"><img src="/img/no-image.png" /></div>`
		if m.Image.Valid {
			images = `<div class="image"><img src="/` + m.Image.String + `" /><img class="blur" src="/` + m.Image.String + `" /></div>`
		}

		if i == 0 || i == 3 || i == 4 || i == 7 || i == 8 || i == 11 {
			templateNews += template.HTML(`<div class="news"><div class="caption">` + m.Caption + `</div><div class="text">` + m.Preview.String + `</div><a href="/данные/новость/` + m.Link + `" class="gradient-button">Читать далее</a>` + images + `</div>`)
		} else {
			templateNews += template.HTML(`<div class="news"><div class="caption">` + m.Caption + `</div><div class="text">` + m.Preview.String + `</div><a href="/данные/новость/` + m.Link + `" class="gradient-button">Читать далее</a></div>`)
		}
	}
}

var templateNewsPagination template.HTML

func prepareNewsPagination() {
	news := material{}
	news.Category = "новость"
	count, e := news.SelectCount()
	if e != nil {
		logger.Println(e.Error())
		return
	}

	templateNewsPagination = template.HTML("")

	count = count / 8

	if count%8 != 0 {
		count++
	}

	if count == 0 {
		return
	}

	templateNewsPagination += template.HTML(`<a class="active" href="javascript:void(0)">` + strconv.Itoa(1) + `</a>`)

	for i := 1; i < count; i++ {
		templateNewsPagination += template.HTML(`<a href="/страница/` + strconv.Itoa(i) + `">` + strconv.Itoa(i+1) + `</a>`)
	}
}

// Для быстрой вставки объявлений в шаблон HTML главной страницы
var templateNotice template.HTML

func prepareNotice() {
	news := material{}
	news.Category = "объявление"
	e := news.SelectWithContent(0, 50)
	if e != nil {
		logger.Println(e.Error())
		return
	}

	templateNotice = template.HTML("")

	for i, m := range news.Rows {

		images := `<div class="image no"><img src="/img/no-image.png" /></div>`
		if m.Image.Valid {
			images = `<div class="image"><img src="/` + m.Image.String + `" /><img class="blur" src="/` + m.Image.String + `" /></div>`
		}

		templateNotice += template.HTML(`<div class="slide" data-slide="` + strconv.Itoa(i) + `">` + images + `<div class="content"><h2>` + m.Caption + `</h2><div class="text">` + m.Preview.String + `</div><article class="full-text">` + string(m.Content) + `</article><a href="javascript:void(0)" class="gradient-button">Развернуть</a></div></div>`)
	}
}
