/*
 * Copyright 2013-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.apple.xcode.xcodeproj;

import com.facebook.buck.apple.xcode.XcodeprojSerializer;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;

/**
 * Reference to a concrete file.
 */
public class PBXFileReference extends PBXReference {
  private static ImmutableMap<String, String> fileTypeToFileTypeIdentifiers =
      ImmutableMap.<String, String>builder()
          .put("a", "archive.ar")
          .put("app", "wrapper.application")
          .put("bdic", "file")
          .put("bin", "archive.macbinary")
          .put("bmp", "image.bmp")
          .put("bundle", "wrapper.cfbundle")
          .put("c", "sourcecode.c.c")
          .put("cc", "sourcecode.cpp.cpp")
          .put("cpp", "sourcecode.cpp.cpp")
          .put("css", "text.css")
          .put("cxx", "sourcecode.cpp.cpp")
          .put("dart", "sourcecode")
          .put("dylib", "compiled.mach-o.dylib")
          .put("exp", "sourcecode.exports")
          .put("framework", "wrapper.framework")
          .put("fsh", "sourcecode.glsl")
          .put("gyp", "sourcecode")
          .put("gypi", "text")
          .put("h", "sourcecode.c.h")
          .put("hxx", "sourcecode.cpp.h")
          .put("icns", "image.icns")
          .put("java", "sourcecode.java")
          .put("jar", "archive.jar")
          .put("jpeg", "image.jpeg")
          .put("jpg", "image.jpeg")
          .put("js", "sourcecode.javascript")
          .put("json", "text.json")
          .put("m", "sourcecode.c.objc")
          .put("mm", "sourcecode.cpp.objcpp")
          .put("nib", "wrapper.nib")
          .put("o", "compiled.mach-o.objfile")
          .put("octest", "wrapper.cfbundle")
          .put("pdf", "image.pdf")
          .put("pl", "text.script.perl")
          .put("plist", "text.plist.xml")
          .put("pm", "text.script.perl")
          .put("png", "image.png")
          .put("proto", "text")
          .put("py", "text.script.python")
          .put("r", "sourcecode.rez")
          .put("rez", "sourcecode.rez")
          .put("rtf", "text.rtf")
          .put("s", "sourcecode.asm")
          .put("storyboard", "file.storyboard")
          .put("strings", "text.plist.strings")
          .put("tif", "image.tiff")
          .put("tiff", "image.tiff")
          .put("tcc", "sourcecode.cpp.cpp")
          .put("ttf", "file")
          .put("vsh", "sourcecode.glsl")
          .put("xcassets", "folder.assetcatalog")
          .put("xcconfig", "text.xcconfig")
          .put("xcodeproj", "wrapper.pb-project")
          .put("xcdatamodel", "wrapper.xcdatamodel")
          .put("xcdatamodeld", "wrapper.xcdatamodeld")
          .put("xib", "file.xib")
          .put("y", "sourcecode.yacc")
          .put("zip", "archive.zip")
          .build();

  private Optional<String> lastKnownFileType;

  public PBXFileReference(String name, String path, SourceTree sourceTree) {
    super(name, path, sourceTree);

    // this is necessary to prevent O(n^2) behavior in xcode project loading
    String fileType = fileTypeToFileTypeIdentifiers.get(Files.getFileExtension(name));
    lastKnownFileType = Optional.fromNullable(fileType);
  }

  public Optional<String> getLastKnownFileType() {
    return lastKnownFileType;
  }

  public void setLastKnownFileType(Optional<String> lastKnownFileType) {
    this.lastKnownFileType = lastKnownFileType;
  }

  @Override
  public String isa() {
    return "PBXFileReference";
  }

  @Override
  public void serializeInto(XcodeprojSerializer s) {
    super.serializeInto(s);

    if (lastKnownFileType.isPresent()) {
      s.addField("lastKnownFileType", lastKnownFileType.get());
    }
  }
}

@@ -3447,33 +3447,32 @@
 nal%3CString%3E 
-lastKnown
+explicit
 FileType;%0A%0A 
@@ -3751,25 +3751,24 @@
 ));%0A    
-lastKnown
+explicit
 FileType
@@ -3837,25 +3837,24 @@
 ing%3E get
-LastKnown
+Explicit
 FileType
@@ -3869,25 +3869,24 @@
  return 
-lastKnown
+explicit
 FileType
@@ -3913,17 +3913,16 @@
  set
-LastKnown
+Explicit
 File
@@ -3943,25 +3943,24 @@
 String%3E 
-lastKnown
+explicit
 FileType
@@ -3972,25 +3972,24 @@
    this.
-lastKnown
+explicit
 FileType
@@ -3991,25 +3991,24 @@
 eType = 
-lastKnown
+explicit
 FileType
@@ -4188,25 +4188,24 @@
     if (
-lastKnown
+explicit
 FileType
@@ -4238,25 +4238,24 @@
 dField(%22
-lastKnown
+explicit
 FileType
@@ -4261,17 +4261,16 @@
 e%22, 
-lastKnown
+explicit
 File

Change lastKnownFileType to explicitFileType in generated Xcode projects