func handleGetFavorites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	favorites, queryerr := models.LoadFavoritesFromDB(token.UsersId)
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	error := favorites.LoadDishesFromDB()
	if error != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, favorites.Dishes)
	if queryerr2 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	servers.WriteResponse(w, usersDishes, servers.STATUS_SUCCESS)
}
func handleAuth(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	// get data from json params
	var encodedParams AuthEncodedParams
	err := json.NewDecoder(r.Body).Decode(&encodedParams)
	if err != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	encodeData, err64 := base64.StdEncoding.DecodeString(encodedParams.Hash)
	if err64 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	// get real data by RSA decoding
	decoded, decodeError := crypt.RSADecrypt([]byte(encodeData))
	if decodeError != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_CANT_DECRYPT)
		return
	}

	var authData AuthData
	err = json.Unmarshal(decoded, &authData)
	if err != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	// getting wordpress user by login
	var wpUser *models.WPUser
	wpUser, err = models.GetWPUserByLogin(authData.UserLogin)
	if err != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_USER_NOT_FOUND)
		return
	}

	// checking passwords
	if crypt.CheckPassword(authData.UserPass, wpUser.UserPass) {
		fmt.Println("User " + wpUser.Name + " authorized.")
	} else {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_WRONG_PASS)
		return
	}

	role, roleError := models.GetUserRoleById(wpUser.Id)
	if roleError != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_CANT_GET_ROLE)
		return
	}

	token, expire, tokenerr := models.GenerateTokenAndSave(wpUser.Id, authData.DeviceId, authData.DeviceType, role, strings.Split(r.RemoteAddr, ":")[0])
	if tokenerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_CANT_GENERATE_TOKEN)
		return
	}

	tokenRes := TokenResult{token, expire}
	servers.WriteResponse(w, tokenRes, servers.STATUS_SUCCESS)
}
func handleGetCategoreis(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	cat := GetCategoriesResponse{[]string{"First", "Second", "Garnishes", "Salads", "Desserts"}}

	servers.WriteResponse(w, cat, servers.STATUS_SUCCESS)
}
func handleGetUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	userResult := GetUserResponse{token.User.Name, token.User.Email, token.Role}

	servers.WriteResponse(w, userResult, servers.STATUS_SUCCESS)
}
func handleGetOffices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	off := GetOfficesResponse{[]string{"Paris", "Not Paris"}}

	servers.WriteResponse(w, off, servers.STATUS_SUCCESS)
}
func handleGetProviders(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	providers, queryerr := models.GetAllProviders()
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetProvidersResponse{providers}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
func handleGetOrders(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	var params GetOrdersParams
	decodeerr := json.NewDecoder(r.Body).Decode(&params)
	if decodeerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	menus, queryerr := models.GetOrdersForUserBetween(token.UsersId, params.StartDate, params.EndDate)
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetOrdersResponse{menus}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
func handleGetMenus(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	var params MenuParams
	decodeErr := json.NewDecoder(r.Body).Decode(&params)
	if decodeErr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	menus, queryErr := models.GetMenuByProvider(params.ProviderId, params.Date)
	if queryErr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetMenusResponse{menus}
	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
func handlePush(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	if r.Header.Get("Authorization") != statictoken {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_TOKEN_NOT_FOUND)
		return
	}

	var params models.PushMessage
	decodeerr := json.NewDecoder(r.Body).Decode(&params)
	if decodeerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	fmt.Println("Params:", params)

	go func(params models.PushMessage) {
		err := notification.SendNotification(&params)
		if err != nil {
			fmt.Println("Sending problem:", err.Error())
		}
	}(params)

	servers.WriteResponse(w, nil, servers.STATUS_SUCCESS)
}
func handleRemoveFavorites(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	idStr := ps.ByName("dishId")

	dishId, convErr := strconv.ParseUint(idStr, 10, 64)
	if convErr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	favorites, queryerr := models.LoadFavoritesFromDB(token.UsersId)
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	favorites.RemoveDish(dishId)
	queryerr = favorites.LoadDishesFromDB()
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	queryerr = favorites.SaveToDB()
	if queryerr != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, favorites.Dishes)
	if queryerr2 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	servers.WriteResponse(w, usersDishes, servers.STATUS_SUCCESS)
}
// params:
// 	providerId
// 	date
func handleGetDishesByProviderAndDate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	q := r.URL.Query()
	idstr := q.Get("providerId")

	providerId, converr1 := strconv.ParseUint(idstr, 10, 64)
	if converr1 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	dateStr := q.Get("date")
	date, converr2 := strconv.ParseInt(dateStr, 10, 64)
	if converr2 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_PARAMS_DECODE)
		return
	}

	dishes, queryerr1 := models.GetDishesByProviderAndDate(providerId, date)
	if queryerr1 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	usersDishes, queryerr2 := models.GetUsersDishes(token.UsersId, dishes)
	if queryerr2 != nil {
		servers.WriteResponse(w, nil, servers.STATUS_ERR_QUERY_PROBLEM)
		return
	}

	res := GetDishesResponse{usersDishes}

	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}
func handleGetOrder(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	token, err := servers.AuthenticateWithHeader(r.Header.Get("Authorization"), strings.Split(r.RemoteAddr, ":")[0])
	if err != servers.STATUS_ERR_OK {
		servers.WriteResponse(w, nil, err)
		return
	}

	q := r.URL.Query()
	var res interface{}

	orderIdStr := q.Get("order_id")

	orderFromDateStr := q.Get("from_date")
	orderToDateStr := q.Get("to_date")

	orderDateStr := q.Get("order_date")

	if len(orderIdStr) > 0 {
		orderId, converr := strconv.ParseInt(orderIdStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		order, queryerr := models.GetOrderListForUserAndId(token.UsersId, uint64(orderId))
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListResponse{*order}
	} else if len(orderFromDateStr) > 0 && len(orderToDateStr) > 0 {
		orderFromDate, converr := strconv.ParseInt(orderFromDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		orderToDate, converr := strconv.ParseInt(orderToDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		orders, queryerr := models.GetOrderListsForUserBetween(token.UsersId, orderFromDate, orderToDate)
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListsResponse{orders}
	} else if len(orderDateStr) > 0 {
		orderDate, converr := strconv.ParseInt(orderDateStr, 10, 64)
		if converr != nil {
			servers.WriteResponse(w, converr.Error(), servers.STATUS_ERR_PARAMS_DECODE)
			return
		}
		order, queryerr := models.GetOrderListForUserAndDate(token.UsersId, orderDate)
		if queryerr != nil {
			servers.WriteResponse(w, queryerr.Error(), servers.STATUS_ERR_QUERY_PROBLEM)
			return
		}
		res = GetOrderListResponse{*order}
	}

	servers.WriteResponse(w, res, servers.STATUS_SUCCESS)
}