<?php
require_once('Database.php');
class ActiveLang extends Database{
    public $Id;
    public $LangName;
    public $Pricing;

  public function InsertRecord(){
    $Query = "INSERT INTO  ActiveLang (LangName, Pricing) 
                            VALUES('$this->LangName',  $this->Pricing)";
    return $this->ExecuteQuery($Query);
  }

  public function InsertRecordPrice($from, $to, $price){
    $Query = "INSERT INTO  LangPrice (FromLang, ToLang, Price) 
                            VALUES($from,  $to, $price)";
    return $this->ExecuteQuery($Query);
  }

  public function DeleteRecordPrice($id){
    $Query = "DELETE FROM LangPrice WHERE Id= $id";
    return $this->ExecuteQuery($Query);
  }

    public function GetRecords(){
        $Query = "SELECT *  FROM ActiveLang WHERE Status = 1 ORDER BY  FIELD(LangName,'es','en') DESC";
        return $this->ExecuteReader($Query);
    }

  public function GetRecordsPrices(){
    $Query = "SELECT LP.Id, A2.LangName AS FromLang,A.LangName AS ToLang, LP.Price   FROM LangPrice LP 
        INNER JOIN ActiveLang A ON LP.ToLang = A.Id
        INNER JOIN ActiveLang A2 ON LP.FromLang = A2.Id
        WHERE A.Status = 1 AND A2.Status = 1";
    return $this->ExecuteReader($Query);
  }

  public function GetRecord(){
    $Query = "SELECT *  FROM ActiveLang WHERE Id = $this->Id";
    return $this->ExecuteUniqueReader($Query);
  }

  public function GetRecordPricing($from, $to, $pages){
    error_reporting(1);
    require ("../../core/utils/Constants.php");
    $allowed = $LANGS;
    if(in_array($from, $allowed) && in_array($to, $allowed)){
      if($pages<=60){
        return $pages*$BASE_60;
      }elseif($pages>60 && $pages<=150){
        return $pages*$BASE_150;
      }elseif($pages>151 && $pages<=400){
        return $pages*$BASE_400;
      }else{
        return $pages*$BASE_MORE;
      }
    }
    return -1;
  }

  public function UpdateLangPrice($id, $price){
    $Query = "UPDATE LangPrice SET Price = $price WHERE Id = $id";
    return $this->ExecuteQuery($Query);
  }
}