Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Text
Imports System.Xml.Linq
Imports System.Web.SessionState
Imports System.IO
''' <summary>
''' Summary description for FakeDataHelper
''' </summary>
Public Class FakeDataHelper
Public Const PhotosFolder As String = "~\Content\Photos"
Private Const CreatedHotelsTitlesSID As String = "CHT"
Private Const TitleMinLength As Integer = 2
Private Const TitleMaxLength As Integer = 4
Private Const MaxRoomPrice As Integer = 500
Private Const MinRoomPrice As Integer = 10
Private Const MinSearchResultsCount As Integer = 5
Public Sub New()
Random = New Random()
Photos = New List(Of String)(System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath(PhotosFolder)))
End Sub
Private privateRandom As Random
Protected Property Random() As Random
Get
Return privateRandom
End Get
Set(ByVal value As Random)
privateRandom = value
End Set
End Property
Private privatePhotos As List(Of String)
Protected Property Photos() As List(Of String)
Get
Return privatePhotos
End Get
Set(ByVal value As List(Of String))
privatePhotos = value
End Set
End Property
' Countries
Private Function GetRandomCity(ByVal country As Country) As String
Return country.Cities(Random.Next(0, country.Cities.Count))
End Function
Private Function GetRandomCountry() As Country
Return Data.Instance.Countries(Random.Next(0, Data.Instance.Countries.Count))
End Function
Public Function GetPopularCountries(ByVal countries As List(Of Country), ByVal count As Integer) As IEnumerable(Of Country)
Return ( _
From country In countries _
Where country.IsPopular _
Order By Random.Next() _
Select country).Take(count).ToList()
End Function
Public Function GetPopularity(ByVal isPopular As Boolean) As Integer
If isPopular Then
Return Random.Next(1, 5)
Else
Return 0
End If
End Function
Public Function GetHotels(ByVal searchQuery As SearchQuery, ByVal titleGroups As IEnumerable(Of XElement)) As IEnumerable(Of Hotel)
Dim count As Integer = Random.Next(15, 50)
Return GetHotels(searchQuery, titleGroups, count)
End Function
Public Function GetHotels(ByVal searchQuery As SearchQuery, ByVal titleGroups As IEnumerable(Of XElement), ByVal count As Integer) As IEnumerable(Of Hotel)
Dim hotels As New List(Of Hotel)()
For i As Integer = 0 To count - 1
hotels.Add(GenerateHotel(searchQuery, titleGroups))
Next i
Return hotels
End Function
Public Sub UpdateHotelsList(ByVal titleGroups As List(Of XElement))
Dim data As Data = Data.Instance
Dim matchedHotelsCount As Integer = data.Hotels.FindAll(Function(h) data.CurrentQuery.IsMatch(h)).Count
If matchedHotelsCount < MinSearchResultsCount Then
data.Hotels.AddRange(GetHotels(data.CurrentQuery, titleGroups, Random.Next(MinSearchResultsCount, 15)))
End If
End Sub
Protected ReadOnly Property CreatedHotelsTitles() As List(Of String)
Get
Dim session As HttpSessionState = HttpContext.Current.Session
If session(CreatedHotelsTitlesSID) Is Nothing Then
session(CreatedHotelsTitlesSID) = New List(Of String)()
End If
Return CType(session(CreatedHotelsTitlesSID), List(Of String))
End Get
End Property
' Hotel generator
Private Function GenerateHotel(ByVal query As SearchQuery, ByVal titleGroups As IEnumerable(Of XElement)) As Hotel
Dim data As Data = Data.Instance
Dim country As Country
Dim city As String
If String.IsNullOrEmpty(query.Country) Then
If query.IsExclusive Then
country = GetPopularCountries(data.Countries, 1).First()
Else
country = GetRandomCountry()
End If
city = GetRandomCity(country)
Else
country = data.Countries.Find(Function(c) c.Name = query.Country)
If String.IsNullOrEmpty(query.City) Then
city = GetRandomCity(country)
Else
city = query.City
End If
End If
Dim hotel As New Hotel(GetHotelTitle(titleGroups))
hotel.Country = country
hotel.City = city
If query.Stars.Length > 0 Then
hotel.Stars = query.Stars(Random.Next(0, query.Stars.Length))
Else
hotel.Stars = Random.Next(2, 6)
End If
hotel.RoomServices.AddRange(GenerateServices(data.RoomServices, query.RoomServices))
hotel.HotelServices.AddRange(GenerateServices(data.HotelServices, query.HotelServices))
hotel.Rooms.AddRange(GenerateRooms(query, hotel))
hotel.ImageUrl = Path.GetFileName(Photos(Random.Next(0, Photos.Count)))
hotel.Rating = CDec(Random.Next(3, 5) + Random.NextDouble())
hotel.VoteCounter = Random.Next(10, 25)
hotel.IsExclusive = query.IsExclusive
hotel.DescriptionIndex = GenerateDescriptionIndex()
Return hotel
End Function
Private Function GenerateDescriptionIndex() As Integer()
Return New Integer() { Random.Next(0, Data.Instance.CommonDescriptions.Count), Random.Next(0, Data.Instance.HotelDescriptions.Count), Random.Next(0, Data.Instance.RoomDescriptions.Count) }
End Function
Private Function GenerateServices(ByVal services As List(Of String), ByVal impServices() As String) As IEnumerable(Of String)
Dim res As List(Of String) = services.OrderBy(Function(s) Random.Next()).Take(Random.Next(1, services.Count + 1)).ToList()
For Each impserv As String In impServices
If (Not res.Contains(impserv)) Then
res.Add(impserv)
End If
Next impserv
Return res
End Function
Private Function GenerateRooms(ByVal query As SearchQuery, ByVal hotel As Hotel) As IEnumerable(Of Room)
Dim power As Integer = 4
Dim koeff As Double = CDbl(MaxRoomPrice - MinRoomPrice) / Math.Pow(5, power)
Dim minPrice As Integer = CInt(Fix(CDbl(MinRoomPrice) + Math.Pow(hotel.Stars - 1, power) * koeff))
Dim maxPrice As Integer = CInt(Fix(CDbl(MinRoomPrice) + Math.Pow(hotel.Stars, power) * koeff))
Dim rooms As New List(Of Room)()
For i As Integer = 0 To Data.Instance.RoomTypes.Count - 1
Dim price As Integer = minPrice + Random.Next(minPrice, maxPrice)
rooms.Add(New Room(Data.Instance.RoomTypes(i), 3, Data.Instance.RoomTypesMaxChildrenCount(i), price))
Next i
Return rooms
End Function
' Title generator
Private Function GetHotelTitle(ByVal titleGroups As IEnumerable(Of XElement)) As String
Dim title As String = String.Empty
For i = 0 To 9
title = GenerateTitle(titleGroups)
If (Not CreatedHotelsTitles.Contains(title)) Then
CreatedHotelsTitles.Add(title)
Return title
End If
Next i
Return title
End Function
Private Function GenerateTitle(ByVal titleGroups As IEnumerable(Of XElement)) As String
Dim sb As New StringBuilder()
Dim res As String = String.Empty
Dim groups As New List(Of XElement)(titleGroups)
Dim length As Integer = Random.Next(TitleMinLength, TitleMaxLength + 1)
For i As Integer = 0 To length - 1
Dim group As XElement = groups(Random.Next(0, groups.Count))
If group.Attribute("OnlyOneWord") IsNot Nothing Then
groups.Remove(group)
End If
If group.Attribute("LastWord") IsNot Nothing Then
sb.Append(GetRandomWord(group))
Else
sb.Insert(0, GetRandomWord(group) & " ")
End If
Next i
Return sb.ToString().Trim(" "c)
End Function
Private Function GetRandomWord(ByVal cGroup As XElement) As String
Return ( _
From word In cGroup.Elements() _
Order By Random.Next() _
Select word.Value).First()
End Function
End Class