<?
namespace admin\factory;

class Integrations {

  // sellasist_product_select_delete
  static public function sellasist_product_select_delete( int $product_id ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'sellasist_product_id' => null, 'sellasist_product_name' => null ], [ 'id' => $product_id ] );
  }

  // sellasist_product_select_save
  static public function sellasist_product_select_save( int $product_id, $sellasist_product_id, $sellasist_product_name ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'sellasist_product_id' => $sellasist_product_id, 'sellasist_product_name' => \S::remove_special_chars( $sellasist_product_name ) ], [ 'id' => $product_id ] );
  }

  // apilo delete product linking
  static public function apilo_product_select_delete( int $product_id ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'apilo_product_id' => null, 'apilo_product_name' => null ], [ 'id' => $product_id ] );
  }

  // baselinker delete product linking
  static public function baselinker_product_select_delete( int $product_id ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'baselinker_product_id' => null, 'baselinker_product_name' => null ], [ 'id' => $product_id ] );
  }

  // apilo product select save
  static public function apilo_product_select_save( int $product_id, $apilo_product_id, $apilo_product_name ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'apilo_product_id' => $apilo_product_id, 'apilo_product_name' => \S::remove_special_chars( $apilo_product_name ) ], [ 'id' => $product_id ] );
  }

  static public function baselinker_product_select_save( int $product_id, $baselinker_product_id, $baselinker_product_name ) {
    global $mdb;
    return $mdb -> update( 'pp_shop_products', [ 'baselinker_product_id' => $baselinker_product_id, 'baselinker_product_name' => $baselinker_product_name ], [ 'id' => $product_id ] );
  }

  // get settings for shoppro integration
  static public function shoppro_settings( $name = '' )
  {
    global $mdb;

    if ( $name )
    {
      return $mdb -> get( 'pp_shop_shoppro_settings', 'value', [ 'name' => $name ] );
    }

    $results = $mdb -> query( 'SELECT * FROM pp_shop_shoppro_settings' ) -> fetchAll( \PDO::FETCH_ASSOC );
    $settings = [];
    foreach ( $results as $result )
    {
      $settings[$result['name']] = $result['value'];
    }

    return $settings;
  }

  // get settings for the sellasist integration
  static public function sellasist_settings( $name = '' )
  {
    global $mdb;

    if ( $name )
    {
      return $mdb -> get( 'pp_shop_sellasist_settings', 'value', [ 'name' => $name ] );
    }

    $results = $mdb -> query( 'SELECT * FROM pp_shop_sellasist_settings' ) -> fetchAll( \PDO::FETCH_ASSOC );
    $settings = [];
    foreach ( $results as $result )
    {
      $settings[$result['name']] = $result['value'];
    }

    return $settings;
  }

  // get settings for the Baselinker integration
  static public function baselinker_settings( $name = '' )
  {
    global $mdb;

    if ( $name )
    {
      return $mdb -> get( 'pp_shop_baselinker_settings', 'value', [ 'name' => $name ] );
    }

    $results = $mdb -> query( 'SELECT * FROM pp_shop_baselinker_settings' ) -> fetchAll( \PDO::FETCH_ASSOC );
    $settings = [];
    foreach ( $results as $result )
    {
      $settings[$result['name']] = $result['value'];
    }

    return $settings;
  }

  // get settings for the APILO plugin
  static public function apilo_settings( $name = '' )
  {
    global $mdb;

    if ( $name )
    {
      return $mdb -> get( 'pp_shop_apilo_settings', 'value', [ 'name' => $name ] );
    }

    $results = $mdb -> query( 'SELECT * FROM pp_shop_apilo_settings' ) -> fetchAll( \PDO::FETCH_ASSOC );
    $settings = [];
    foreach ( $results as $result )
    {
      $settings[$result['name']] = $result['value'];
    }

    return $settings;
  }

  // save settings for shoppro integration
  static public function shoppro_settings_save( $field_id, $value )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_shop_shoppro_settings', [ 'name' => $field_id ] ) ) {
      $mdb -> update( 'pp_shop_shoppro_settings', [ 'value' => $value ], [ 'name' => $field_id ] );
    } else {
      $mdb -> insert( 'pp_shop_shoppro_settings', [ 'name' => $field_id, 'value' => $value ] );
    }
    return true;
  }

  // save settings for the sellasist integration
  static public function sellasist_settings_save( $field_id, $value )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_shop_sellasist_settings', [ 'name' => $field_id ] ) ) {
      $mdb -> update( 'pp_shop_sellasist_settings', [ 'value' => $value ], [ 'name' => $field_id ] );
    } else {
      $mdb -> insert( 'pp_shop_sellasist_settings', [ 'name' => $field_id, 'value' => $value ] );
    }
    return true;
  }

  // save settings for the Baselinker integration
  static public function baselinker_settings_save( $field_id, $value )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_shop_baselinker_settings', [ 'name' => $field_id ] ) ) {
      $mdb -> update( 'pp_shop_baselinker_settings', [ 'value' => $value ], [ 'name' => $field_id ] );
    } else {
      $mdb -> insert( 'pp_shop_baselinker_settings', [ 'name' => $field_id, 'value' => $value ] );
    }
    return true;
  }

  // save settings for the APILO plugin
  static public function apilo_settings_save( $field_id, $value )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_shop_apilo_settings', [ 'name' => $field_id ] ) ) {
      $mdb -> update( 'pp_shop_apilo_settings', [ 'value' => $value ], [ 'name' => $field_id ] );
    } else {
      $mdb -> insert( 'pp_shop_apilo_settings', [ 'name' => $field_id, 'value' => $value ] );
    }
    return true;
  }

  // authorization in apilo.com
  static public function apilo_authorization( $client_id, $client_secret, $authorization_code )
  {
    global $mdb;

    $url = "https://projectpro.apilo.com/rest/auth/token/";

    $postData['grantType'] = 'authorization_code';
    $postData['token'] = $authorization_code;

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $postData ) );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, [
      "Authorization: Basic " . base64_encode( $client_id . ":" . $client_secret ),
      "Accept: application/json"
    ] );

    $response = curl_exec( $ch );
    if ( curl_errno( $ch ) ) {
      return false;
    }
    curl_close( $ch );
    $response = json_decode( $response, true );

    $access_token = $response['accessToken'];
    $refresh_token = $response['refreshToken'];
    $access_token_expire_at = $response['accessTokenExpireAt'];
    $refresh_token_expire_at = $response['refreshTokenExpireAt'];

    self::apilo_settings_save( 'access-token', $access_token );
    self::apilo_settings_save( 'refresh-token', $refresh_token );
    self::apilo_settings_save( 'access-token-expire-at', $access_token_expire_at );
    self::apilo_settings_save( 'refresh-token-expire-at', $refresh_token_expire_at );

    return true;
  }

  // get access token or refresh it apilo.com
  static public function apilo_get_access_token()
  {
    global $mdb;

    $apilo_settings = self::apilo_settings();

    $date1 = new \DateTime( $apilo_settings['access-token-expire-at'] );
    $date2 = new \DateTime( date( 'Y-m-d H:i:s' ) );

    if ( $date1 < $date2 )
    {
      $post_data = [
        'grantType' => 'refresh_token',
        'token' => $apilo_settings['refresh-token']
      ];
      $ch = curl_init( "https://projectpro.apilo.com/rest/auth/token/" );
      curl_setopt( $ch, CURLOPT_HTTPHEADER, [
        "Authorization: Basic " . base64_encode( $apilo_settings['client-id'] . ":" . $apilo_settings['client-secret'] ),
        "Accept: application/json"
      ] );
      curl_setopt( $ch, CURLOPT_POST, true );
      curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post_data ) );
      curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

      $response = curl_exec( $ch );
      if ( curl_errno( $ch ) ) {
        return false;
      }
      curl_close( $ch );
      $response = json_decode( $response, true );

      $access_token = $response['accessToken'];
      $refresh_token = $response['refreshToken'];
      $access_token_expire_at = $response['accessTokenExpireAt'];
      $refresh_token_expire_at = $response['refreshTokenExpireAt'];

      if ( $access_token == '' ) {
        return false;
      }

      self::apilo_settings_save( 'access-token', $access_token );
      self::apilo_settings_save( 'refresh-token', $refresh_token );
      self::apilo_settings_save( 'access-token-expire-at', $access_token_expire_at );
      self::apilo_settings_save( 'refresh-token-expire-at', $refresh_token_expire_at );

      return $access_token;
    }

    return $apilo_settings['access-token'];
  }
}