package back

import (
	"database/sql"
	"errors"
)

func prepareQueriesMenu() []string {
	var e error
	var ebox []string

	queries["Insert#Menu#"], e = db.Prepare(`INSERT INTO "Menu"
		("Parent", "Caption", "Link", "Position", "Active", "Category")
		VALUES ($1, $2, $3, $4, TRUE, $5)`)
	if e != nil {
		ebox = append(ebox, "Insert#Menu# - "+e.Error())
	}
	queries["Delete#Menu#"], e = db.Prepare(`DELETE FROM "Menu" WHERE "ID"=$1`)
	if e != nil {
		ebox = append(ebox, "Delete#Menu# - "+e.Error())
	}
	queries["Update#Menu#"], e = db.Prepare(`UPDATE "Menu"
		SET "Caption"=$2, "Link"=$3 WHERE "ID"=$1`)
	if e != nil {
		ebox = append(ebox, "Update#Menu# - "+e.Error())
	}
	queries["Update#Menu#Active"], e = db.Prepare(`UPDATE "Menu"
		SET "Active"=$2 WHERE "ID"=$1`)
	if e != nil {
		ebox = append(ebox, "Update#Menu#Active - "+e.Error())
	}
	queries["Select#Menu#"], e = db.Prepare(`SELECT
		"ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Active"=TRUE AND "Category"=$1
		ORDER BY "Parent" DESC, "Position", "Caption"`)
	if e != nil {
		ebox = append(ebox, "Select#Menu# - "+e.Error())
	}
	queries["Select#Menu#All"], e = db.Prepare(`SELECT
		"ID", "Parent", "Caption", "Link", "Position", "Active" FROM "Menu" WHERE "Category"=$1
		ORDER BY "Parent" DESC, "Position", "Caption"`)
	if e != nil {
		ebox = append(ebox, "Select#Menu#All - "+e.Error())
	}

	return ebox
}

type menu struct {
	ID       int32          `json:"ID"`
	Parent   sql.NullInt32  `json:"Parent"`
	Caption  string         `json:"Caption"`
	Link     sql.NullString `json:"Link"`
	Position int32          `json:"Position"`
	Active   bool           `json:"Active"`
	Categoty string         `json:"Category"`

	Rows []menu
}

func (m *menu) Insert() error {
	stmt, ok := queries["Insert#Menu#"]
	if !ok {
		return errors.New("Запрос Insert#Menu# не найден")
	}

	_, e := stmt.Exec(m.Parent, m.Caption, m.Link, m.Position, m.Categoty)
	if e != nil {
		return e
	}

	return nil
}

func (m *menu) Delete() error {
	stmt, ok := queries["Delete#Menu#"]
	if !ok {
		return errors.New("Запрос Delete#Menu# не найден")
	}

	_, e := stmt.Exec(m.ID)
	if e != nil {
		return e
	}

	return nil
}

func (m *menu) Update() error {
	stmt, ok := queries["Update#Menu#"]
	if !ok {
		return errors.New("Запрос Update#Menu# не найден")
	}

	_, e := stmt.Exec(m.ID, m.Caption, m.Link)
	if e != nil {
		return e
	}

	return nil
}

func (m *menu) UpdateActive() error {
	stmt, ok := queries["Update#Menu#Active"]
	if !ok {
		return errors.New("Запрос Update#Menu#Active не найден")
	}

	_, e := stmt.Exec(m.ID, m.Active)
	if e != nil {
		return e
	}

	return nil
}

func (m *menu) Select() error {

	stmt, ok := queries["Select#Menu#"]
	if !ok {
		return errors.New("Запрос Select#Menu# не найден")
	}

	rows, e := stmt.Query(m.Categoty)
	if e != nil {
		return e
	}
	defer rows.Close()

	for rows.Next() {

		e = rows.Scan(
			&m.ID,
			&m.Parent,
			&m.Caption,
			&m.Link,
			&m.Position,
			&m.Active,
		)
		if e != nil {
			return e
		}

		m.Rows = append(m.Rows, menu{
			ID:       m.ID,
			Parent:   m.Parent,
			Caption:  m.Caption,
			Link:     m.Link,
			Position: m.Position,
			Active:   m.Active,
		})
	}

	return nil
}

func (m *menu) SelectAll() error {

	stmt, ok := queries["Select#Menu#All"]
	if !ok {
		return errors.New("Запрос Select#Menu#All не найден")
	}

	rows, e := stmt.Query(m.Categoty)
	if e != nil {
		return e
	}
	defer rows.Close()

	for rows.Next() {

		e = rows.Scan(
			&m.ID,
			&m.Parent,
			&m.Caption,
			&m.Link,
			&m.Position,
			&m.Active,
		)
		if e != nil {
			return e
		}

		m.Rows = append(m.Rows, menu{
			ID:       m.ID,
			Parent:   m.Parent,
			Caption:  m.Caption,
			Link:     m.Link,
			Position: m.Position,
			Active:   m.Active,
		})
	}

	return nil
}
