/*
 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
 *
 * 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.
 */

#include <algorithm>
#include <regex>

#include "json_helper.hpp"
#include "layers.hpp"
#include "util.hpp"

namespace wm {

using json = nlohmann::json;

layer::layer(nlohmann::json const &j) {
   if (j["type"] == "range") {
      this->id_min = j["first_surface_id"];
      this->id_max = j["last_surface_id"];
   } else {
      this->id_min = this->id_max = j["surface_id"];
   }
   this->role = j["role"];
   this->name = j["name"];
   this->layer_id = j["layer_id"];
   this->rect = genivi::full_rect;
   if (j["area"]["type"] == "rect") {
      auto jr = j["area"]["rect"];
      this->rect = genivi::rect{
         jr["width"], jr["height"], jr["x"], jr["y"],
      };
   }
   auto split_layouts = j.find("split_layouts");
   if (split_layouts != j.end()) {
      auto &sls = j["split_layouts"];
      // this->layouts.reserve(sls.size());
      std::transform(std::cbegin(sls), std::cend(sls),
                     std::back_inserter(this->layouts), [this](json const &sl) {
                        struct split_layout l {
                           sl["name"], sl["main_match"], sl["sub_match"],
                              sl.value<int>("priority", 0)
                        };
                        logdebug(
                           "layer %d add split_layout \"%s\" (main: \"%s\") (sub: "
                           "\"%s\") (prio: %d)", this->layer_id,
                           l.name.c_str(), l.main_match.c_str(),
                           l.sub_match.c_str(), l.prio);
                        return l;
                     });
      //std::sort(std::begin(this->layouts), std::end(this->layouts),
      //          [](struct split_layout const &a, struct split_layout const &b) {
      //             return a.prio < b.prio;
      //          });
   }
}

struct result<struct layer_map> to_layer_map(nlohmann::json const &j) {
   try {
      layer_map stl{};
      auto m = j["mappings"];

      std::transform(std::cbegin(m), std::cend(m),
                     std::inserter(stl.mapping, stl.mapping.end()),
                     [](nlohmann::json const &j) { return layer(j); });

      // XXX: add sanity checks here?
      // * check for double IDs
      // * check for double names/roles

      stl.layers.reserve(m.size());
      std::transform(
         std::cbegin(stl.mapping), std::cend(stl.mapping),
         std::back_inserter(stl.layers), [&stl](struct layer const &k) {
            stl.roles.emplace_back(std::make_pair(k.role, k.layer_id));
            return unsigned(k.layer_id);
         });

      // XXX need to sort layers?
      for (auto i : stl.mapping) {
         if (i.name.empty()) {
            return Err<struct layer_map>("Found mapping w/o name");
         }
         if (i.layer_id == -1 || i.id_min == -1 || i.id_max == -1) {
            return Err<struct layer_map>("Found invalid/unset IDs in mapping");
         }
      }

      auto msi = j.find("main_surface");
      if (msi != j.end()) {
         stl.main_surface_name = msi->value("surface_role", "");
         stl.main_surface =
            stl.main_surface_name.empty() ? int((*msi)["surface_id"]) : -1;
      }

      // Check lookup
      auto jtests = j.value("tests", json());

      if (!jtests.empty()) {
         logdebug("Embedded tests...");
         std::vector<std::pair<int, int>> tests;
         tests.reserve(jtests.size());
         std::transform(std::cbegin(jtests), std::cend(jtests),
                        std::back_inserter(tests), [](json const &j) {
                           return std::make_pair(j["surface_id"],
                                                 j["expect_layer_id"]);
                        });

         for (auto sid : tests) {
            int lid = stl.get_layer_id(sid.first).value_or(-1);
            logdebug("this=%d, that=%d, expect=%d", sid.first, lid, sid.second);
            if (lid != sid.second) {
               return Err<layer_map>("ID Map embedded test failed!");
            }
         }
      }

      return Ok(stl);
   } catch (std::exception &e) {
      return Err<struct layer_map>(e.what());
   }
}

// Helper to allow std::lower_bound with a int key only
inline bool
   operator<(struct layer const &a, int b) {
   return a.id_max < b;
}

namespace {
optional<layer> get_surface_id_to_layer(struct layer_map const *s2l,
                                        int surface_id) {
   auto i = std::lower_bound(std::cbegin(s2l->mapping), std::cend(s2l->mapping),
                             surface_id);

   if (i != s2l->mapping.end()) {
      // std::less only checks for layer::id_max, so check
      // that we are actually inside of an interval here.
      if (i->id_min <= surface_id) {
         return optional<layer>(*i);
      }
   }

   return nullopt;
}
}  // namespace

optional<int> layer_map::get_layer_id(int surface_id) {
   auto e = get_surface_id_to_layer(this, surface_id);
   if (!e) {
      auto i = this->surfaces.find(surface_id);
      if (i != this->surfaces.end()) {
         return optional<int>(int(i->second));
      }
      return nullopt;
   }
   return optional<int>(e->layer_id);
}

optional<int> layer_map::get_layer_id(std::string const &role) {
   for (auto const &r : this->roles) {
      auto re = std::regex(r.first);
      if (std::regex_match(role, re)) {
         logdebug("role %s matches layer %d", role.c_str(), r.second);
         return optional<int>(r.second);
      }
   }
   logdebug("role %s does NOT match any layer", role.c_str());
   return nullopt;
}

optional<genivi::rect> layer_map::get_layer_rect(int surface_id) {
   auto e = get_surface_id_to_layer(this, surface_id);
   return e ? optional<genivi::rect>(e->rect) : nullopt;
}

json layer::to_json() const {
   auto is_full = this->rect == genivi::full_rect;

   json r{};
   if (is_full) {
      r = {{"type", "full"}};
   } else {
      r = {{"type", "rect"},
           {"rect",
            {{"x", this->rect.x},
             {"y", this->rect.y},
             {"width", this->rect.w},
             {"height", this->rect.h}}}};
   }

   return {
      {"id_min", this->id_min},     {"id_max", this->id_max},
      {"name", this->name},         {"role", this->role},
      {"layer_id", this->layer_id}, {"area", r},
   };
}

json layer_map::to_json() const {
   json j{};
   for (auto const &i : this->mapping) {
      j.push_back(i.to_json());
   }
   return j;
}

}  // namespace wm