<!doctype html>
<html>
<head>
  <title>%graph% for package %package%</title>

  <script type="text/javascript" src="%vis_dir%/dist/vis.js"></script>
  <link href="%vis_dir%/dist/vis.css" rel="stylesheet" type="text/css" />
  <link href="%dep_graph_dir%/dep_graph.css" rel="stylesheet" type="text/css" />

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>


  <style type="text/css">
    #mynetwork {
      width: 100%;
      height: 800px;
      border: 1px solid lightgray;
    }
  </style>

</head>
<body>

<table>
<tr>
  <th>OBS package name</th>
  <td>
<input type="text" name="node_name" id="node_name" OnKeyPress="Javascript: OnKeyPress_node_name(event);" />
<button onclick="FocusNode()">Find</button>
  </td>
</table>

<div id="config"></div>
<div id="mynetwork"></div>

<script type="text/javascript">
<?php
function CallRestAPI($method, $url, $user="", $pw="", $data = false)
{
  $curl = curl_init();

  switch ($method)
  {
    case "POST":
      curl_setopt($curl, CURLOPT_POST, 1);

      if ($data)
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
      break;
    case "PUT":
      curl_setopt($curl, CURLOPT_PUT, 1);
      break;
    default:
      if ($data)
        $url = sprintf("%s?%s", $url, http_build_query($data));
  }

  // Optional Authentication:
  if ( $user != "" ) {
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "$user:$pw");
  }

  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($curl);

  curl_close($curl);

  return $result;
}

function GetBuildStatusFromOBS($obs_api_url, $project, $repo, $arch, $username, $pw) {
  $url = $obs_api_url . "/build/". $project . "/_result?repository=" . $repo . "&arch=" . $arch;
  $response = CallRestAPI("GET", $url, $username, $pw);
  $xml_data = simplexml_load_string($response);
  $build_status = array();
  foreach ($xml_data->result->status as $key => $value) {
    if( $key == "status" ) {
      $pkg = $value["package"];
      $status=$value["code"];
      $build_status["$pkg"] = "$status";
    }
  }

  return $build_status;
}

function GetBuildStatusFromDB($mysql_ip, $mysql_username, $mysql_pw, $mysql_db, $mysql_build_target_id) {
  $conn = new mysqli($mysql_ip, $mysql_username, $mysql_pw, $mysql_db);

  if( $conn->connect_error ) {
    die("connection failed: ". $conn->connect_error);
  }

  $sql = "SELECT info_package.package_name,build_package.build_status FROM build_package,info_package WHERE build_package.last_flag='Y' AND build_package.info_package_id=info_package.id AND build_package.build_target_id=" . $mysql_build_target_id;
#echo $sql;
  $result = $conn->query($sql);

  $build_status = array();
  if( $result->num_rows > 0 ) {
    while($row = $result->fetch_assoc()) {
#echo "package_name : ". $row["package_name"] . ", build_status:" . $row["build_status"] . "<br>";
      $build_status[$row["package_name"]] = strtolower($row["build_status"]);
    }
  }
  $conn->close();

  return $build_status;
}

if ( ! isset($_GET["build_target_id"]) ) {
  $obs_api_url="%obs_api_url%";
  $obs_username="%obs_username%";
  $obs_pw="%obs_pw%";
  $obs_project="%obs_project%";
  $obs_repo="%obs_repo%";
  $obs_arch="%obs_arch%";

  $build_status = GetBuildStatusFromOBS($obs_api_url, $obs_project, $obs_repo, $obs_arch, $obs_username, $obs_pw);

  print "var package_buildinfo = {";
  foreach ($build_status as $key => $value) {
    print "\"".$key."\": \"".$value."\",";
  }
  print "};";

} else {
  // get build status from DB.
  $mysql_ip = "%mysql_ip%";
  $mysql_username = "%mysql_username%";
  $mysql_pw = "%mysql_pw%";
  $mysql_db = "%mysql_db%";
  $mysql_build_target_id = $_GET["build_target_id"];

  $build_status = GetBuildStatusFromDB($mysql_ip, $mysql_username, $mysql_pw, $mysql_db, $mysql_build_target_id);

  print "var package_buildinfo = {";
  foreach ($build_status as $key => $value) {
    print "\"".$key."\": \"".$value."\",";
  }
  print "};";

}

?>
</script>


<script type="text/javascript" src=%src.js%></script>
<script type="text/javascript" src="%dep_graph_dir%/dep_graph_simple.js"></script>
<script type="text/javascript" src="%dep_graph_dir%/dep_graph_common.js"></script>

</body>
</html>
