<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_AvaloniaUseExternalMSBuild>$(AvaloniaUseExternalMSBuild)</_AvaloniaUseExternalMSBuild>
    <_AvaloniaUseExternalMSBuild Condition="'$(_AvaloniaForceInternalMSBuild)' == 'true'">false</_AvaloniaUseExternalMSBuild>
    <AvaloniaXamlReportImportance Condition="'$(AvaloniaXamlReportImportance)' == ''">low</AvaloniaXamlReportImportance>
    <_AvaloniaPatchComInterop Condition="'$(_AvaloniaPatchComInterop)' == ''">false</_AvaloniaPatchComInterop>
    <_AvaloniaSkipXamlCompilation Condition="'$(_AvaloniaSkipXamlCompilation)' == ''">false</_AvaloniaSkipXamlCompilation>
  </PropertyGroup>

  <!-- Unfortunately we have to update default items in .targets since custom nuget props are improted before Microsoft.NET.Sdk.DefaultItems.props -->
  <ItemGroup Condition="'$(EnableDefaultItems)'=='True'">
    <Compile Update="**\*.paml.cs">
      <DependentUpon>%(Filename)</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Update="**\*.axaml.cs">
      <DependentUpon>%(Filename)</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <None Remove="**\*.axaml" />
    <None Remove="**\*.paml" />
  </ItemGroup>
  
  <UsingTask TaskName="GenerateAvaloniaResourcesTask"
             AssemblyFile="$(AvaloniaBuildTasksLocation)"
             />

  <UsingTask TaskName="CompileAvaloniaXamlTask"
             AssemblyFile="$(AvaloniaBuildTasksLocation)"
             />


  <Target Name="AddAvaloniaResources" BeforeTargets="ResolveReferences">
    <PropertyGroup>
      <AvaloniaResourcesTemporaryFilePath Condition="'$(AvaloniaResourcesTemporaryFilePath)' == ''">$(IntermediateOutputPath)/Avalonia/resources</AvaloniaResourcesTemporaryFilePath>
    </PropertyGroup>
    <ItemGroup>
      <EmbeddedResource Include="$(AvaloniaResourcesTemporaryFilePath)">
        <LogicalName>!AvaloniaResources</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
  </Target>
  
  <PropertyGroup>
    <BuildAvaloniaResourcesDependsOn>$(BuildAvaloniaResourcesDependsOn);AddAvaloniaResources;ResolveReferences;_GenerateAvaloniaResourcesDependencyCache</BuildAvaloniaResourcesDependsOn>
  </PropertyGroup>
  
  <Target Name="_GenerateAvaloniaResourcesDependencyCache" BeforeTargets="GenerateAvaloniaResources">
    <ItemGroup>
      <CustomAdditionalGenerateAvaloniaResourcesInputs Include="$(IntermediateOutputPath)/Avalonia/Resources.Inputs.cache" />
    </ItemGroup>
    
    <Hash ItemsToHash="@(AvaloniaResource);@(AvaloniaXaml);$(MSBuildAllProjects)">
      <Output TaskParameter="HashResult" PropertyName="AvaloniaResourcesDependencyHash" />
    </Hash>

    <MakeDir Directories="$(IntermediateOutputPath)/Avalonia" />
    <WriteLinesToFile Overwrite="true" File="$(IntermediateOutputPath)/Avalonia/Resources.Inputs.cache" Lines="$(AvaloniaResourcesDependencyHash)" WriteOnlyWhenDifferent="True" />
  </Target>
  
  <Target Name="GenerateAvaloniaResources" 
          BeforeTargets="CoreCompile;CoreResGen"
          Inputs="@(AvaloniaResource);@(AvaloniaXaml);@(CustomAdditionalGenerateAvaloniaResourcesInputs);$(MSBuildAllProjects)"
          Outputs="$(AvaloniaResourcesTemporaryFilePath)"
	  DependsOnTargets="$(BuildAvaloniaResourcesDependsOn)">
    <ItemGroup>
        <AvaloniaResource Include="@(AvaloniaXaml)"/>
    </ItemGroup>
    <GenerateAvaloniaResourcesTask
      Condition="'$(_AvaloniaUseExternalMSBuild)' != 'true'"
      Output="$(AvaloniaResourcesTemporaryFilePath)"
      Root="$(MSBuildProjectDirectory)"
      Resources="@(AvaloniaResource)"
      EmbeddedResources="@(EmbeddedResources)"
      ReportImportance="$(AvaloniaXamlReportImportance)"/>
    <Exec 
      Condition="'$(_AvaloniaUseExternalMSBuild)' == 'true'"
      Command="dotnet msbuild /nodereuse:false $(MSBuildProjectFile) /t:GenerateAvaloniaResources /p:_AvaloniaForceInternalMSBuild=true /p:Configuration=$(Configuration) /p:TargetFramework=$(TargetFramework) /p:BuildProjectReferences=false"/>

  </Target>

  <Target
    Name="CompileAvaloniaXaml"
    AfterTargets="AfterCompile"
    Condition="Exists('@(IntermediateAssembly)') And $(DesignTimeBuild) != true And $(EnableAvaloniaXamlCompilation) != false"
    >
    <PropertyGroup>
      <AvaloniaXamlReferencesTemporaryFilePath Condition="'$(AvaloniaXamlReferencesTemporaryFilePath)' == ''">$(IntermediateOutputPath)/Avalonia/references</AvaloniaXamlReferencesTemporaryFilePath>
      <AvaloniaXamlOriginalCopyFilePath Condition="'$(AvaloniaXamlOriginalCopyFilePath)' == ''">$(IntermediateOutputPath)/Avalonia/original.dll</AvaloniaXamlOriginalCopyFilePath>
      <AvaloniaXamlIlVerifyIl Condition="'$(AvaloniaXamlIlVerifyIl)' == ''">false</AvaloniaXamlIlVerifyIl>
      <AvaloniaXamlIlDebuggerLaunch Condition="'$(AvaloniaXamlIlDebuggerLaunch)' == ''">false</AvaloniaXamlIlDebuggerLaunch> 
    </PropertyGroup>
    <WriteLinesToFile
      Condition="'$(_AvaloniaForceInternalMSBuild)' != 'true'"
      File="$(AvaloniaXamlReferencesTemporaryFilePath)"
      Lines="@(ReferencePathWithRefAssemblies)"
      Overwrite="true" />
    <CompileAvaloniaXamlTask
      Condition="'$(_AvaloniaUseExternalMSBuild)' != 'true'"
      AssemblyFile="@(IntermediateAssembly)"
      ReferencesFilePath="$(AvaloniaXamlReferencesTemporaryFilePath)"
      OriginalCopyPath="$(AvaloniaXamlOriginalCopyFilePath)"
      ProjectDirectory="$(MSBuildProjectDirectory)"
      VerifyIl="$(AvaloniaXamlIlVerifyIl)"
      ReportImportance="$(AvaloniaXamlReportImportance)"
      AssemblyOriginatorKeyFile="$(AssemblyOriginatorKeyFile)"
      SignAssembly="$(SignAssembly)"
      DelaySign="$(DelaySign)"
      EnableComInteropPatching="$(_AvaloniaPatchComInterop)"
      SkipXamlCompilation="$(_AvaloniaSkipXamlCompilation)"
      DebuggerLaunch="$(AvaloniaXamlIlDebuggerLaunch)"
    />
    <Exec
      Condition="'$(_AvaloniaUseExternalMSBuild)' == 'true'"
      Command="dotnet msbuild /nodereuse:false $(MSBuildProjectFile) /t:CompileAvaloniaXaml /p:_AvaloniaForceInternalMSBuild=true /p:Configuration=$(Configuration) /p:TargetFramework=$(TargetFramework) /p:BuildProjectReferences=false"/>
  </Target>

  
  <ItemGroup>
    <UpToDateCheckInput Include="@(AvaloniaResource)" />
    <UpToDateCheckInput Include="@(AvaloniaXaml)" />
  </ItemGroup>
</Project>
