<?php

namespace Functions;

use Classes\Database;
use \DateTime;

class Apps extends Database
{
    public function get_apps($where = NULL, $order_by = NULL, $offset = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM apps";

        if (!is_null($where) && !is_array($where)) return FALSE;
        if (is_array($where)) $sql .= " WHERE {$where[0]}=?";
        if (!is_null($order_by)) $sql .= " ORDER BY $order_by";
        if (!is_null($offset)) $sql .= " OFFSET $offset";
        if (!is_null($limit)) $sql .= " LIMIT $limit";
        $this->prepare($sql);
        if (is_array($where)) $this->execute([$where[1]]);
        return $this->resultStr();

    }

    public function get_app($by, $id)
    {

        $where = [$by, $id];
        $result = $this->get_apps($where);
        return ((count($result) > 0) ? $result : FALSE);

    }

    public function get_by_appname($app_id)
    {
        return $this->get_app('ca_app', $app_id);
    }

    public function get_by_id($id)
    {
        return $this->get_app('id', $id);
    }

    public function add($values)
    {
        if (!is_array($values)) return FALSE;
        if (count($values) <> 6) return FALSE;

        $sql = "INSERT INTO apps (ca_app, label, metricValues, estByCountry, Ad_Unit, Ad_Format	) VALUES (?,?,?,?,?,?)";
        $this->prepare($sql)->execute($values);

        return $this->lastInsertId();
    }


    public function update($id, $changes)
    {
        if (!is_array($changes)) return FALSE;
        $keys_sql = "";
        $values = [];
        foreach ($changes as $key => $value) {
            $keys_sql .= "$key=?, ";
            $values[] = $value;
        }
        $keys_sql = substr($keys_sql, 0, -2);
        $sql = "UPDATE  apps SET $keys_sql WHERE id=$id";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }

    public function get_app_income($app_id)
    {
        $today_income = 0;
        $yester_day_income = 0;
        $month_income = 0;
        $last_month_income = 0;
        $app = $this->get_by_id($app_id)[0];
        $metric_values = json_decode($app->metricValues, true);
        $today_income += $metric_values["today"]["ESTIMATED_EARNINGS"]["microsValue"];
        $yester_day_income += $metric_values["yesterday"]["ESTIMATED_EARNINGS"]["microsValue"];
        $month_income += $metric_values["this_month"]["ESTIMATED_EARNINGS"]["microsValue"];
        if (isset($metric_values["last_month"])) {
            $last_month_income += $metric_values["last_month"]["ESTIMATED_EARNINGS"]["microsValue"];
        }

        return [$today_income, $yester_day_income, $month_income, $last_month_income, $app->label];
    }

public function get_app_est_earn_by_countries($app_id)
    {
        $app = $this->get_by_id($app_id)[0];
        $estByCountry = json_decode($app->estByCountry, true);
        $output = "";
        $extra_function = new \Functions\Extra();
        foreach ($estByCountry as $country => $values){

            $output .= $extra_function->getFlags($country)." $country : \n";
            $output .= "Earn : ".number_format($values["ESTIMATED_EARNINGS"]["microsValue"]/1000000,2)."$"."\n";
            $output .= "Impr : ".number_format($values["IMPRESSIONS"]["integerValue"]/1000,2)."K\n";
            $output .= "eCPM : ".number_format($values["OBSERVED_ECPM"]["microsValue"]/1000000,2)."$"."\n\n";

        }

        return $output;
    }


}