<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Makanan extends REST_Controller
{

  public function __construct()
  {
    parent::__construct();
    //$this->load->model("ModelAuth");
  }

  public function makanan_post()
  {
    $data = array(
      'kategori' => $this->post("kategori"),
      'bahan_makanan' => $this->post("bahan_makanan"),
      'kuantitas' => $this->post("kuantitas"),
      'besaran' => $this->post("besaran"),

    );

    if ($this->db->get_where("bahan_makanan", array('bahan_makanan' => $this->post("bahan_makanan")))->num_rows() < 1) {
      if ($this->db->get_where("bahan_makanan", array('besaran' => $this->post("besaran")))->num_rows() < 1) {
        if ($this->db->insert('bahan_makanan', $data)) {
          $message = array(
            'status' => 200,
            'message' => "konten berhasil ditambahkan"
          );
        } else {
          $message = array(
            'status' => 500,
            'message' => "INTERNAL_SERVER_ERROR"
          );
        }
      } else {
        $message = array(
          'status' => 502,
          'message' => "Penulis Sudah Digunakan"
        );
      }
    } else {
      $message = array(
        'status' => 501,
        'message' => "Judul Sudah Digunakan"
      );
    }
    $this->response(array('response' => $data, 'message' => $message));
  }

  public function makanan_get()
  {
    // echo "Coba";
    $this->db->select('bahan_makanan.*, kategori.nama_kategori');
    $this->db->from('bahan_makanan');
    $this->db->join('kategori', 'kategori.id_kategori = bahan_makanan.kategori');
    $data = $this->db->get();
    if ($data->num_rows() > 0) {
      $message = array(
        'status' => $this->config->item('http_response_ok'),
        'message' => "OK"
      );
      $data = $data->result();
    } else {
      $data = array();
      $message = array(
        'status' => $this->config->item('http_response_ok_no_content'),
        'message' => "NO_CONTENT"
      );
    }
    $this->response(array('response' => $data, 'message' => $message));
  }

  public function konsumsi_get()
  {
    $id_user = $this->input->get("id_user");
    $waktu = $this->input->get("waktu");
    $keterangan = $this->input->get("keterangan");

    $this->db->select('
    bahan_makanan.nama_makanan, 
    bahan_makanan.besaran, 
    konsumsi_detail.kuantitas, 
    (bahan_makanan.energi * konsumsi_detail.kuantitas) AS kalori, 
    (bahan_makanan.karbohidrat * konsumsi_detail.kuantitas) AS karbohidrat, 
    (bahan_makanan.protein * konsumsi_detail.kuantitas) AS protein, 
    (bahan_makanan.lemak * konsumsi_detail.kuantitas) AS lemak, 
    (bahan_makanan.besi * konsumsi_detail.kuantitas) AS besi, 
    (bahan_makanan.vitamina * konsumsi_detail.kuantitas) AS vitamina, 
    (bahan_makanan.vitaminc * konsumsi_detail.kuantitas) AS vitaminc,
    konsumsi.keterangan');
    $this->db->from('konsumsi');
    $this->db->join('konsumsi_detail', 'konsumsi_detail.id_konsumsi = konsumsi.idkonsumsi');
    $this->db->join('bahan_makanan', 'bahan_makanan.id_makanan = konsumsi_detail.id_makanan');

    $this->db->where('konsumsi.user_id_user', $id_user);
    $this->db->where('DATE(konsumsi.waktu)', $waktu);
    if ($keterangan != null && $keterangan != "") {
      $this->db->where('konsumsi.keterangan', $keterangan);
    }

    $data = $this->db->get();

    $dbUser = $this->db->get_where("user", array('id_user' => $id_user))->row();
    $kalori = 0;
    $umur = date_diff(date_create($dbUser->tgl_lahir), date_create('now'))->y;
    $dbKalori = $this->db->from("kalori")
      ->where("awal <=", $umur)
      ->where("akhir >=", $umur)
      ->where("jenis_kelamin", $dbUser->jekel)
      ->get()->row();
    if ($dbKalori != null) {
      $kalori = $dbKalori->energi;
    }

    $dataUser = array(
      "user"            => $dbUser,
      "umur"            => $umur,
      "kalori"          => $kalori,
    );

    if ($data->num_rows() > 0) {
      $message = array(
        'status' => $this->config->item('http_response_ok'),
      );
      $data = $data->result();
    } else {
      $data = array();
      $message = array(
        'status' => $this->config->item('http_response_ok_no_content'),
        'message' => "NO_CONTENT"
      );
    }
    $this->response(array('response' => $data, 'dataUser' => $dataUser, 'message' => $message));
  }

  public function kalorikonsumsi_get()
  {
    $id_user = $this->input->get("id_user");
    $waktu = $this->input->get("waktu");
    $keterangan = $this->input->get("keterangan");

    $this->db->select('
    bahan_makanan.nama_makanan, 
    bahan_makanan.besaran, 
    konsumsi_detail.kuantitas, 
    (bahan_makanan.energi * konsumsi_detail.kuantitas) AS kalori, 
    (bahan_makanan.karbohidrat * konsumsi_detail.kuantitas) AS karbohidrat, 
    (bahan_makanan.protein * konsumsi_detail.kuantitas) AS protein, 
    (bahan_makanan.lemak * konsumsi_detail.kuantitas) AS lemak, 
    (bahan_makanan.besi * konsumsi_detail.kuantitas) AS besi, 
    (bahan_makanan.vitamina * konsumsi_detail.kuantitas) AS vitamina, 
    (bahan_makanan.vitaminc * konsumsi_detail.kuantitas) AS vitaminc,
    konsumsi.keterangan');
    $this->db->from('konsumsi');
    $this->db->join('konsumsi_detail', 'konsumsi_detail.id_konsumsi = konsumsi.idkonsumsi');
    $this->db->join('bahan_makanan', 'bahan_makanan.id_makanan = konsumsi_detail.id_makanan');

    $this->db->where('konsumsi.user_id_user', $id_user);
    $this->db->where('DATE(konsumsi.waktu)', $waktu);
    if ($keterangan != null && $keterangan != "") {
      $this->db->where('konsumsi.keterangan', $keterangan);
    }

    $data = $this->db->get();

    $total_kalori = 0;
    if ($data->num_rows() > 0) {
      $message = array(
        'status' => $this->config->item('http_response_ok'),
      );
      $data = $data->result();
      foreach ($data as $key => $value) {
        $total_kalori += $value->kalori;
      }
    } else {
      $data = array();
      $message = array(
        'status' => $this->config->item('http_response_ok_no_content'),
        'message' => "NO_CONTENT"
      );
    }
    $dbUser = $this->db->get_where("user", array('id_user' => $id_user))->row();
    $kalori = 0;
    $umur = date_diff(date_create($dbUser->tgl_lahir), date_create('now'))->y;
    $dbKalori = $this->db->from("kalori")
      ->where("awal <=", $umur)
      ->where("akhir >=", $umur)
      ->get()->row();
    if ($dbKalori != null) {
      $kalori = $dbKalori->energi;
    }
    // if ($umur >= 1 && $umur <= 3) {
    //   $kalori = 1350;
    // } else if ($umur >= 4 && $umur <= 6) {
    //   $kalori = 1400;
    // } else if ($umur >= 7 && $umur <= 9) {
    //   $kalori = 1650;
    // } else if ($umur >= 10 && $umur <= 12) {
    //   $kalori = 2000;
    // } else if ($umur >= 13 && $umur <= 15) {
    //   $kalori = 2400;
    // } else if ($umur >= 16) {
    //   $kalori = 2650;
    // }
    $sarapan_kalori = $kalori * 40 / 100;
    $siang_kalori = $kalori * 40 / 100;
    $malam_kalori = $kalori * 10 / 100;
    $dataUser = array(
      "user"            => $dbUser,
      "umur"            => $umur,
      "kalori"          => $kalori,
      "konsumsi_kalori" => $kalori,
      "karbo"           => $kalori * 30 / 100,
      "sayur"           => $kalori * 30 / 100,
      "lauk"            => $kalori * 20 / 100,
      "buah"            => $kalori * 20 / 100,
      "waktu"           => strtolower($keterangan),
    );
    //lowercase keterangan
    if (strtolower($keterangan) == "sarapan") {
      $dataUser["konsumsi_kalori"] = $sarapan_kalori;
      $dataUser["karbo"] = $sarapan_kalori * 30 / 100;
      $dataUser["sayur"] = $sarapan_kalori * 30 / 100;
      $dataUser["lauk"] = $sarapan_kalori * 20 / 100;
      $dataUser["buah"] = $sarapan_kalori * 20 / 100;
    } else if (strtolower($keterangan) == "makan siang") {
      $dataUser["konsumsi_kalori"] = $siang_kalori;
      $dataUser["karbo"] = $siang_kalori * 30 / 100;
      $dataUser["sayur"] = $siang_kalori * 30 / 100;
      $dataUser["lauk"] = $siang_kalori * 20 / 100;
      $dataUser["buah"] = $siang_kalori * 20 / 100;
    } else if (strtolower($keterangan) == "makan malam") {
      $dataUser["konsumsi_kalori"] = $malam_kalori;
      $dataUser["karbo"] = $malam_kalori * 30 / 100;
      $dataUser["sayur"] = $malam_kalori * 30 / 100;
      $dataUser["lauk"] = $malam_kalori * 20 / 100;
      $dataUser["buah"] = $malam_kalori * 20 / 100;
    }

    $persentase_kalori = ($total_kalori / $dataUser["konsumsi_kalori"]) * 100;
    $keterangan = "Keterangan Kalori Anda : ";
    if ($persentase_kalori < 70) {
      $keterangan = "Kalori Anda Defisit Berat";
    } else if ($persentase_kalori >= 70 && $persentase_kalori < 80) {
      $keterangan = "Kalori Anda Defisit Sedang";
    } else if ($persentase_kalori >= 80 && $persentase_kalori < 90) {
      $keterangan = "Kalori Anda Defisit Ringan";
    } else if ($persentase_kalori >= 90 && $persentase_kalori < 120) {
      $keterangan = "Kalori Anda Ideal";
    } else if ($persentase_kalori >= 120) {
      $keterangan = "Kalori Anda Berlebihan";
    }
    $dataUser["persentase_kalori"] = round($persentase_kalori);
    $dataUser["keterangan"] = $keterangan;

    $this->response(array('response' => $data, 'datauser' => $dataUser, 'message' => $message));
  }

  public function allKonsumsi_get()
  {
    $id_user = $this->input->get("id_user");
    $startDate = date("Y-m-d", strtotime($this->input->get("start")));
    $endDate = date("Y-m-d", strtotime($this->input->get("end")));

    $resData = array();

    $this->db->select('
    konsumsi.waktu,
    bahan_makanan.nama_makanan,
    kategori.id_kategori, 
    kategori.nama_kategori, 
    bahan_makanan.besaran, 
    konsumsi_detail.kuantitas, 
    (bahan_makanan.energi * konsumsi_detail.kuantitas) AS kalori');
    $this->db->from('konsumsi');
    $this->db->join('konsumsi_detail', 'konsumsi_detail.id_konsumsi = konsumsi.idkonsumsi');
    $this->db->join('bahan_makanan', 'bahan_makanan.id_makanan = konsumsi_detail.id_makanan');
    $this->db->join('kategori', 'kategori.id_kategori = bahan_makanan.kategori');
    $this->db->where('konsumsi.user_id_user', $id_user);
    $this->db->where('DATE(konsumsi.waktu) >=', $startDate);
    $this->db->where('DATE(konsumsi.waktu) <=', $endDate);
    $data = $this->db->get();

    $tempkarbo = 0;
    $templauk = 0;
    $tempsayur = 0;
    $tempbuah = 0;

    $maxval = 0;

    // selisih hari
    $tanggal_1 = date_create($startDate);
    $tanggal_2 = date_create($endDate);
    $selisih  = date_diff($tanggal_1, $tanggal_2);
    $selisih = $selisih->days;
    $dataQuery = $data->result();
    for ($i = 0; $i <= $selisih; $i++) {
      $tempdate = date("Y-m-d", strtotime("+" . $i . " day", strtotime($startDate)));
      foreach ($dataQuery as $value) {
        if (date("Y-m-d", strtotime($value->waktu)) == $tempdate) {
          if ($value->id_kategori == 1) {
            $tempkarbo += $value->kalori;
          } else if ($value->id_kategori == 2) {
            $templauk += $value->kalori;
          } else if ($value->id_kategori == 3) {
            $tempsayur += $value->kalori;
          } else if ($value->id_kategori == 4) {
            $tempbuah += $value->kalori;
          }
        }
      }
      $resData[] = array(
        "tanggal" => date("d-M-Y", strtotime($tempdate)),
        "karbo" => round($tempkarbo),
        "lauk" => round($templauk),
        "sayur" => round($tempsayur),
        "buah" => round($tempbuah),
        "kalori" => round($tempkarbo + $templauk + $tempsayur + $tempbuah)
      );

      if ($tempkarbo > $maxval) {
        $maxval = $tempkarbo;
      }
      if ($templauk > $maxval) {
        $maxval = $templauk;
      }
      if ($tempsayur > $maxval) {
        $maxval = $tempsayur;
      }
      if ($tempbuah > $maxval) {
        $maxval = $tempbuah;
      }

      $tempdate = date("Y-m-d", strtotime($tempdate));
      $tempkarbo = 0;
      $templauk = 0;
      $tempsayur = 0;
      $tempbuah = 0;
    }

    $resGraf = array();
    for ($i = 0; $i < sizeof($resData); $i++) {
      $karbo = 0;
      if ($resData[$i]["karbo"] > 0) {
        $karbo = ($resData[$i]["karbo"] / $maxval) * 4;
      }
      $lauk = 0;
      if ($resData[$i]["lauk"] > 0) {
        $lauk = ($resData[$i]["lauk"] / $maxval) * 4;
      }
      $sayur = 0;
      if ($resData[$i]["sayur"] > 0) {
        $sayur = ($resData[$i]["sayur"] / $maxval) * 4;
      }
      $buah = 0;
      if ($resData[$i]["buah"] > 0) {
        $buah = ($resData[$i]["buah"] / $maxval) * 4;
      }
      $resGraf[] = array(
        "tanggal" => $resData[$i]["tanggal"],
        "karbo" => round($karbo, 2),
        "lauk" => round($lauk, 2),
        "sayur" => round($sayur, 2),
        "buah" => round($buah, 2),
      );
    }

    $dataMax = array(
      "karbo" => round($maxval),
      "lauk" => round($maxval),
      "sayur" => round($maxval),
      "buah" => round($maxval),
    );

    if ($data->num_rows() > 0) {
      $message = array(
        'status' => $this->config->item('http_response_ok'),
        'message' => $data
      );
      $data = $resData;
    } else {
      $data = array();
      $message = array(
        'status' => $this->config->item('http_response_ok_no_content'),
        'message' => "NO_CONTENT"
      );
    }
    $this->response(array(
      'response'  => $data,
      'resGraf'   => $resGraf,
      'dataMax'   => $dataMax,
      'message'   => $message
    ));
  }
}
