<?php
//setting header to json 
    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");
    $driverID = $_POST["id"];  // get internal sensor id from js
    $gpio = $_POST["p"];
    $assigned = $_POST["a"];
    $typeID = $_POST["t"];
    $logic = $_POST["l"];
    $name = $_POST["n"];
    $icon = $_POST["i"];

    // $driverID = 3;  // get internal sensor id from js
    // $gpio = 14;
    // $assigned = 1;
    // $typeID = 10;
    // $name = "LED 2";

    require '../php/database.php';
    
    // get pinID and pinTypeID before we update pinsAvailable table
    $query = sprintf("SELECT pinID, pinTypeID, assigned FROM pinsAvailable WHERE driverID = '$driverID' AND gpio = '$gpio'");
    $result = $mysqli->query($query);  // execute query
    $dataOrg = $result->fetch_array(MYSQLI_ASSOC); // get data from query results
    
    // update pin details
    $query = "UPDATE pinsAvailable SET `name`='$name', `assigned`='$assigned', `pinTypeID`='$typeID', `invert`='$logic', `iconID`='$icon' WHERE `pinID`='".$dataOrg['pinID']."'";
    $mysqli->query($query);  // execute query

    // LIGHT MODES
    // clean up light mode channels if necessary --- removing channels
    if ((($dataOrg['assigned'] == 1) & ($assigned == 0) & ($typeID == 11)) || (($dataOrg['pinTypeID'] == 11) & ($typeID != 11))) {        
        // get all modeID's so we can create a channel for each mode
        $query = sprintf("SELECT `modeID` FROM `lightModes`");  // get modeID for all modes
        $result2 = $mysqli->query($query);  // execute query

        while ($row = $result2->fetch_assoc()) {  // go through each light mode            
            // create channel in lightModeDetails table for each pin set as LED type
            $result3 = "DELETE FROM lightModes WHERE pinID='".$dataOrg['pinID']."'";
            if ($mysqli->query($result3) === FALSE) {}
        }
    }
    
    // DOSING PUMPS
    // clean up dosing pumps if necessary --- adding pump
    if ((($dataOrg['assigned'] == 0) & ($assigned == 1 ) & ($typeID == 12)) || (($dataOrg['pinTypeID'] != 12) & ($typeID == 12) & ($assigned == 1))) { 
        // create record in dosingPumps table
        $result3 = $mysqli->query("INSERT IGNORE INTO dosingPumps (dosePinID) VALUES ('".$dataOrg['pinID']."')");
        if ($result3 === false) {echo "SQL error:".$mysqli->error;}
        
        $query = "UPDATE pinsAvailable SET `name`='Add Custom Name' WHERE `pinID`='".$dataOrg['pinID']."'";
        $result = $mysqli->query($query);  // execute query
    }
    // clean up dosing pumps if necessary --- removing pump
    else if ((($dataOrg['assigned'] == 1) & ($assigned == 0) & ($typeID == 12)) || (($dataOrg['pinTypeID'] == 12) & ($typeID != 12))) {
        // delete record in dosingPumps table
        $result3 = "DELETE FROM dosingPumps WHERE dosePinID='".$dataOrg['pinID']."'";
        if ($mysqli->query($result3) === FALSE) {}
    }
    
    // DOSING PUMP STIRRERS
    // clean up dosing pump stirrers if necessary --- adding stirrer
    if ((($dataOrg['assigned'] == 0) & ($assigned == 1 ) & ($typeID == 15)) || (($dataOrg['pinTypeID'] != 15) & ($typeID == 15) & ($assigned == 1))) { 
        // create record in dosingPumps table
        $result3 = $mysqli->query("INSERT IGNORE INTO stirrers (stirrerPinID) VALUES ('".$dataOrg['pinID']."')");
        if ($result3 === false) {echo "SQL error:".$mysqli->error;}
        
        // $query = "UPDATE pinsAvailable SET `name`='Add Custom Name' WHERE `pinID`='".$dataOrg['pinID']."'";
        // $result = $mysqli->query($query);  // execute query
    }
    // clean up dosing pump stirrers if necessary --- removing stirrer
    else if ((($dataOrg['assigned'] == 1) & ($assigned == 0) & ($typeID == 15)) || (($dataOrg['pinTypeID'] == 15) & ($typeID != 15))) {
        
        $query = sprintf("SELECT stirrerID FROM stirrers WHERE stirrerPinID = '".$dataOrg['pinID']."'");
        $result = $mysqli->query($query);  // execute query
        $dataOrg2 = $result->fetch_array(MYSQLI_ASSOC); // get data from query results

        $query = "UPDATE dosingPumps SET `doseStirrerID`='0', `doseStirrer`='0' WHERE `doseStirrerID`='".$dataOrg2['stirrerID']."'";
        $result = $mysqli->query($query);  // execute query

        // delete record in dosingPumps table
        $result3 = "DELETE FROM stirrers WHERE stirrerPinID='".$dataOrg['pinID']."'";
        if ($mysqli->query($result3) === FALSE) {}
    }
    

    // DHT22
    // clean up DHT22 if necessary --- adding DHT22
    // if ((($dataOrg['assigned'] == 0) & ($assigned == 1 ) & ($typeID == 4)) || (($dataOrg['pinTypeID'] != 4) & ($typeID == 4) & ($assigned == 1))) { 
    if (($typeID == 4) & ($dataOrg['pinTypeID'] != 4)) { 
        // create record in ds18b20 table
        if ($dataOrg['assigned'] == 1) {  // add and enable
            $result3 = $mysqli->query("INSERT IGNORE INTO ds18b20 (address, plugged) VALUES ('dht_".$dataOrg['pinID']."', 1)");
        }
        else if ($dataOrg['assigned'] == 0) {  // add and disable
            $result3 = $mysqli->query("INSERT IGNORE INTO ds18b20 (address, plugged) VALUES ('dht_".$dataOrg['pinID']."', 0)");
        }
        if ($result3 === false) {echo "SQL error:".$mysqli->error;}
    }
    // clean up DHT22 channels if necessary --- removing DHT22
    else if (($dataOrg['pinTypeID'] == 4) & ($typeID != 4)) {
        // delete record in ds18b20 table
        $result3 = "DELETE FROM ds18b20 WHERE address='dht_".$dataOrg['pinID']."'";
        if ($mysqli->query($result3) === FALSE) {}
    }
    // clean up DHT22 channels if necessary --- disabling DHT22
    else if (($dataOrg['assigned'] == 1) & ($assigned == 0) & ($typeID == 4)) {
        // delete record in ds18b20 table
        // $result3 = "DELETE FROM ds18b20 WHERE address='".$dataOrg['pinID']."'";
        $result3 = "UPDATE ds18b20 SET `plugged` = '0' WHERE address='dht_".$dataOrg['pinID']."'";
        if ($mysqli->query($result3) === FALSE) {}
    }
    // clean up DHT22 channels if necessary --- enabling DHT22
    else if (($dataOrg['assigned'] == 0) & ($assigned == 1) & ($typeID == 4)) {
        // delete record in ds18b20 table
        // $result3 = "DELETE FROM ds18b20 WHERE address='".$dataOrg['pinID']."'";
        $result3 = "UPDATE ds18b20 SET `plugged` = '1' WHERE address='dht_".$dataOrg['pinID']."'";
        if ($mysqli->query($result3) === FALSE) {}
    }

    // $result->close();
    // $result2->close();
    // $result3->close();
    $mysqli->close();

    
    print json_encode($dataWindow);  // send data so javascript knows this ran
?>