/*
 * Copyright 2016 Damien Carol <damien.carol@gmail.com>
 *
 * 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 hdfs.jsr203;

import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * Implementation of {@link PosixFileAttributeView}.
 */
public class HadoopPosixFileAttributeView extends HadoopFileOwnerAttributeView
    implements PosixFileAttributeView, IAttributeReader, IAttributeWriter {

  private final HadoopPath path;
  /** posix or owner ? */
  private final boolean isPosixView;

  private enum AttrID {
    owner,
    creationTime,
    lastAccessTime,
    lastModifiedTime,
    isDirectory,
    isRegularFile,
    isSymbolicLink,
    isOther, 
    fileKey,
    group,
    permissions
  };

  public HadoopPosixFileAttributeView(HadoopPath path, boolean isPosixView) {
    super(path);
    this.path = path;
    this.isPosixView = isPosixView;
  }

  @Override
  public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime,
      FileTime createTime) throws IOException {
    // TODO Auto-generated method stub

  }

  @Override
  public String name() {
    if (!isPosixView) {
      return super.name();
    }
    return "posix";
  }

  @Override
  public PosixFileAttributes readAttributes() throws IOException {
    Path resolvedPath = path.getRawResolvedPath();
    FileStatus fileStatus = path.getFileSystem().getHDFS()
        .getFileStatus(resolvedPath);
    String fileKey = resolvedPath.toString();
    return new HadoopPosixFileAttributes(this.path.getFileSystem(), fileKey,
        fileStatus);
  }

  @Override
  public void setPermissions(Set<PosixFilePermission> perms)
      throws IOException {
    throw new IOException("Not implemented");
  }

  @Override
  public void setGroup(GroupPrincipal group) throws IOException {
    FileSystem fs = path.getFileSystem().getHDFS();
    fs.setOwner(path.getRawResolvedPath(), null, group.getName());
  }

  @Override
  public Map<String, Object> readAttributes(String attributes,
      LinkOption[] options) throws IOException {
    PosixFileAttributes zfas = readAttributes();
    LinkedHashMap<String, Object> map = new LinkedHashMap<>();
    if ("*".equals(attributes)) {
      for (AttrID id : AttrID.values()) {
        try {
          map.put(id.name(), attribute(id, zfas));
        } catch (IllegalArgumentException x) {
        }
      }
    } else {
      String[] as = attributes.split(",");
      for (String a : as) {
        try {
          map.put(a, attribute(AttrID.valueOf(a), zfas));
        } catch (IllegalArgumentException x) {
        }
      }
    }
    return map;
  }

  @Override
  public void setAttribute(String attr, Object value, LinkOption[] options)
      throws IOException {
    // FIXME Implement HadoopPosixFileAttributeView.setAttribute()
    throw new UnsupportedOperationException();
  }

  Object attribute(AttrID id, PosixFileAttributes hfas) {
    switch (id) {
    case owner:
      return hfas.owner().getName();
    case group:
      return hfas.owner().getName();
    case permissions:
      return hfas.owner().getName();
    default:
      return null;
    }
  }
}

@@ -1478,32 +1478,43 @@
     
-owner,%0A    creationTime,
+// file%0A    lastModifiedTime, %0A    
 %0A   
@@ -1533,47 +1533,53 @@
 ime,
+ 
 %0A    
-lastModified
+%0A    creation
 Time,
+ 
 %0A    
-isDirectory,
+%0A    size,%0A    
 %0A   
@@ -1593,16 +1593,45 @@
 larFile,
+ %0A    %0A    isDirectory, %0A    
 %0A    isS
@@ -1642,16 +1642,22 @@
 licLink,
+ %0A    
 %0A    isO
@@ -1663,16 +1663,21 @@
 Other, %0A
+    %0A
     file
@@ -1684,19 +1684,56 @@
 Key,
+ 
 %0A    
-group,
+%0A    // fileowner%0A    owner,%0A%0A    // posix
 %0A   
@@ -1744,16 +1744,27 @@
 missions
+,%0A    group
 %0A  %7D;%0A%0A 
@@ -2063,43 +2063,49 @@
     
-// TODO Auto-generated method stub%0A
+throw new IOException(%22Not implemented%22);
 %0A  %7D
@@ -3266,32 +3266,16 @@
 es()) %7B%0A
-        try %7B%0A  
         
@@ -3319,65 +3319,8 @@
 ));%0A
-        %7D catch (IllegalArgumentException x) %7B%0A        %7D%0A
     
@@ -3411,24 +3411,8 @@
 ) %7B%0A
-        try %7B%0A  
     
@@ -3467,65 +3467,8 @@
 ));%0A
-        %7D catch (IllegalArgumentException x) %7B%0A        %7D%0A
     

Fix incorrect handling of posix view.