<#@ template language="VB" HostSpecific="true" #>
<#@ import namespace="ICSharpCode.AspNet.Mvc" #>
<#@ import namespace="Microsoft.VisualBasic.Constants" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<# If MvcHost.IsPartialView Then #>
<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl<#= GetViewPageType() #>" %>

<# ElseIf MvcHost.IsContentPage Then #>
<%@ Page Language="VB" MasterPageFile="<#= MvcHost.MasterPageFile #>" Inherits="System.Web.Mvc.ViewPage<#= GetViewPageType() #>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Title" runat="server">
<#= MvcHost.ViewName #>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="<#= MvcHost.PrimaryContentPlaceHolderID #>" runat="server">
<#
		PushIndent(vbTab)
	Else
#>
<%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage<#= GetViewPageType() #>" %>

<!DOCTYPE html>
<html>
	<head runat="server">
		<title><#= MvcHost.ViewName #></title>
	</head>
	<body>
<# 
		PushIndent(vbTab & vbTab)
	End If
#>
<% Using Html.BeginForm() %>
	<%: Html.ValidationSummary(True) %>
	<fieldset>
		<legend><#= MvcHost.ViewDataType.Name #></legend>
		
<# 
	For Each modelProperty As ModelProperty in GetModelProperties()
		If Not modelProperty.IsPrimaryKey Then
#>
		<div class="editor-label">
			<%: Html.LabelFor(Function(model) model.<#= modelProperty.Name #>) %>
		</div>
		<div class="editor-field">
			<%: Html.EditorFor(Function(model) model.<#= modelProperty.Name #>) %>
			<%: Html.ValidationMessageFor(Function(model) model.<#= modelProperty.Name #>) %>
		</div>
		
<#
		End If
	Next
#>
		<p>
			<input type="submit" value="Create"/>
		</p>
	</fieldset>
<% End Using %>
<div>
	<%: Html.ActionLink("Back", "Index") %>
</div>
<#
	If MvcHost.IsPartialView Then
		' Do nothing.
	ElseIf MvcHost.IsContentPage Then
		PopIndent()
#>
</asp:Content>
<#
	Else
		PopIndent()
#>
	</body>
</html>
<# End If #>
<#+
	ReadOnly Property MvcHost As MvcTextTemplateHost
		Get
			Return DirectCast(Host, MvcTextTemplateHost)
		End Get
	End Property
	
	Public Class ModelProperty
		Public Property Name() As String
		Public Property IsPrimaryKey() As Boolean
		
		Public Sub New(ByVal propertyInfo As PropertyInfo)
			Me.Name = propertyInfo.Name
			Me.IsPrimaryKey = IsPrimaryKeyProperty(propertyInfo)
		End Sub
		
		Function IsPrimaryKeyProperty(ByVal propertyInfo As PropertyInfo) As Boolean
			Return IsIdProperty(propertyInfo) Or _
				IsIdPropertyPrefixedWithClassName(propertyInfo)
		End Function
		
		Function IsIdProperty(ByVal propertyInfo As PropertyInfo) As Boolean
			Return IsMatchIgnoringCase(propertyInfo.Name, "id")
		End Function
		
		Function IsIdPropertyPrefixedWithClassName(ByVal propertyInfo As PropertyInfo) As Boolean
			Dim prefixedIdPropertyName As String = propertyInfo.DeclaringType.Name + "id"
			Return IsMatchIgnoringCase(propertyInfo.Name, prefixedIdPropertyName)
		End Function
		
		Shared Function IsMatchIgnoringCase(ByVal a As String, ByVal b as String) As Boolean
			Return String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
		End Function
	End Class
	
	Public Function GetViewPageType() As String
		Dim viewDataTypeName As String = MvcHost.ViewDataTypeName
		If Not String.IsNullOrEmpty(viewDataTypeName) Then
			Return String.Format("(Of {0})", viewDataTypeName)
		End If
		Return String.Empty
	End Function
	
	Public Function GetModelProperties() As IEnumerable(Of ModelProperty)
		Dim properties = New List(Of ModelProperty)()
		For Each propertyInfo As PropertyInfo In MvcHost.GetViewDataTypeProperties()
			properties.Add(New ModelProperty(propertyInfo))
		Next
		Return properties
	End Function
#>