/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.zeppelin.interpreter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import org.apache.zeppelin.common.JsonSerializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.zeppelin.dep.Repository;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.*;

import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;

/**
 *
 */
public class InterpreterInfoSaving implements JsonSerializable {

  private static final Logger LOGGER = LoggerFactory.getLogger(InterpreterInfoSaving.class);

  private static final Gson GSON =  new GsonBuilder().setPrettyPrinting()
          .create();

  public Map<String, InterpreterSetting> interpreterSettings = new HashMap<>();
  public List<Repository> interpreterRepositories = new ArrayList<>();

  public static InterpreterInfoSaving loadFromFile(Path file) throws IOException {
    LOGGER.info("Load interpreter setting from file: {}", file);
    InterpreterInfoSaving infoSaving = null;
    try (BufferedReader json = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
      JsonObject jsonObject = JsonParser.parseReader(json).getAsJsonObject();
      infoSaving = InterpreterInfoSaving.fromJson(jsonObject.toString());

      if (infoSaving != null && infoSaving.interpreterSettings != null) {
        for (InterpreterSetting interpreterSetting : infoSaving.interpreterSettings.values()) {
          JsonObject interpreterSettingJson = jsonObject.getAsJsonObject("interpreterSettings")
              .getAsJsonObject(interpreterSetting.getId());
          List<String> users = InterpreterSetting.extractUsersFromJsonString(interpreterSettingJson.toString());
          interpreterSetting.convertPermissionsFromUsersToOwners(users);
        }
      }
    }
    return infoSaving == null ? new InterpreterInfoSaving() : infoSaving;
  }

  public void saveToFile(Path file) throws IOException {
    if (!Files.exists(file)) {
      Files.createFile(file);
      try {
        Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
        Files.setPosixFilePermissions(file, permissions);
      } catch (UnsupportedOperationException e) {
        // File system does not support Posix file permissions (likely windows) - continue anyway.
        LOGGER.warn("unable to setPosixFilePermissions on '{}'.", file);
      }
    }
    LOGGER.info("Save Interpreter Settings to {}", file);
    IOUtils.write(this.toJson(), new FileOutputStream(file.toFile()), StandardCharsets.UTF_8);
  }

  @Override
  public String toJson() {
    return GSON.toJson(this);
  }

  public static InterpreterInfoSaving fromJson(String json) {
    InterpreterInfoSaving interpreterInfoSaving = GSON.fromJson(json, InterpreterInfoSaving.class);
    if (interpreterInfoSaving.interpreterSettings != null) {
      for (Map.Entry<String, InterpreterSetting> entry : interpreterInfoSaving.interpreterSettings.entrySet()) {
        String key = entry.getKey();
        InterpreterSetting setting = entry.getValue();
        String id = setting == null ? null : setting.getId();
        if (!key.equals(id)) {
          LOGGER.error("Invalid interpreterSettings: key '{}' does not match inner id '{}'.", key, id);
          throw new IllegalArgumentException(
              "interpreterSettings key '" + key + "' does not match inner id '" + id + "'");
        }
      }
    }
    return interpreterInfoSaving;
  }

  // kept for backward compatibility: no custom serializers are needed currently
}
