<?php
namespace controls;
class Campaigns
{
  static public function main_view()
  {
    return \Tpl::view( 'campaigns/main_view', [
      'clients' => \factory\Campaigns::get_clients(),
    ] );
  }

  static public function get_campaigns_list()
  {
    $client_id = (int) \S::get( 'client_id' );
    $include_archived = (int) \S::get( 'include_archived' ) === 1;

    echo json_encode( [ 'campaigns' => \factory\Campaigns::get_campaigns_list( $client_id, !$include_archived ) ] );
    exit;
  }

  static public function get_campaign_history_data_table()
  {
    $campaign_id = \S::get( 'campaign_id' );
    $start = \S::get( 'start' );
    $length = \S::get( 'length' );

    $db_results = \factory\Campaigns::get_campaign_history_data( $campaign_id, $start, $length );
    $recordsTotal = \factory\Campaigns::get_records_total_campaign_history_data( $campaign_id );

    $result = [
      'draw' => \S::get( 'draw' ),
      'recordsTotal' => $recordsTotal,
      'recordsFiltered' => $recordsTotal
    ];

    foreach ( $db_results as $row )
    {
      $result['data'][] = [
        '<input type="checkbox" class="history-check" value="' . $row['id'] . '">',
        $row['date_add'],
        number_format( $row['roas_30_days'], 0, '', ' ' ),
        number_format( $row['roas_all_time'], 0, '', ' ' ),
        \S::number_display( $row['conversion_value'] ),
        \S::number_display( $row['money_spent'] ),
        '',
        $row['bidding_strategy'],
        \S::number_display( $row['budget'] ),
        '<button type="button" class="btn btn-danger btn-sm delete-history-entry" data-id="' . $row['id'] . '" data-date="' . $row['date_add'] . '"><i class="fa fa-trash"></i></button>',
      ];
    }

    echo json_encode( $result );
    exit;
  }

  static public function get_campaign_history_data_table_chart()
  {
    $campaign_id = \S::get( 'campaign_id' );

    $db_result = \factory\Campaigns::get_campaign_history_data( $campaign_id, 0, 1000, true );

    $roas30 = [];
    $roasAllTime = [];
    $budget = [];
    $moneySpent = [];
    $conversionValue = [];
    $dates = [];

    foreach ( $db_result as $row )
    {
      $roas30[] = (float)$row['roas_30_days'];
      $roasAllTime[] = (float)$row['roas_all_time'];
      $budget[] = (float)$row['budget'];
      $moneySpent[] = (float)$row['money_spent'];
      $dates[] = $row['date_add'];
      $conversionValue[] = (float)$row['conversion_value'];
    }

    $chart_data = [
      [
        'name' => 'Roas 30 dni',
        'data' => $roas30,
        'visible' => true
      ], [
        'name' => 'Roas cały okres',
        'data' => $roasAllTime,
        'visible' => true
      ], [
        'name' => 'Budżet',
        'data' => $budget,
        'visible' => false
      ], [
        'name' => 'Wydatki',
        'data' => $moneySpent,
        'visible' => false
      ], [
        'name' => 'Wartość konwersji',
        'data' => $conversionValue,
        'visible' => false
      ]
    ];

    echo json_encode( [
      'chart_data' => $chart_data,
      'dates' => $dates,
      'comments' => \factory\Campaigns::get_campaign_comments( $campaign_id )
    ] );
    exit;
  }

  static public function get_campaign_ad_groups()
  {
    $campaign_id = (int) \S::get( 'campaign_id' );

    if ( $campaign_id <= 0 )
    {
      echo json_encode( [ 'ad_groups' => [] ] );
      exit;
    }

    echo json_encode( [ 'ad_groups' => \factory\Campaigns::get_campaign_ad_groups( $campaign_id ) ] );
    exit;
  }

  static public function get_campaign_phrase_details()
  {
    $campaign_id = (int) \S::get( 'campaign_id' );
    $ad_group_id = (int) \S::get( 'ad_group_id' );

    if ( $campaign_id <= 0 )
    {
      echo json_encode( [ 'search_terms' => [], 'negative_keywords' => [] ] );
      exit;
    }

    echo json_encode( [
      'search_terms' => \factory\Campaigns::get_campaign_search_terms( $campaign_id, $ad_group_id ),
      'negative_keywords' => \factory\Campaigns::get_campaign_negative_keywords( $campaign_id, $ad_group_id )
    ] );
    exit;
  }

  static public function delete_campaign()
  {
    $campaign_id = \S::get( 'campaign_id' );

    if ( !$campaign_id )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie wybrano kampanii' ] );
      exit;
    }

    $result = \factory\Campaigns::delete_campaign( $campaign_id );

    echo json_encode( [ 'success' => $result ? true : false ] );
    exit;
  }

  static public function delete_campaigns()
  {
    $ids = \S::get( 'ids' );

    if ( empty( $ids ) || !is_array( $ids ) )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie wybrano kampanii' ] );
      exit;
    }

    $ids = array_map( 'intval', $ids );
    $ids = array_filter( $ids, function( $id ) { return $id > 0; } );

    if ( empty( $ids ) )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie wybrano kampanii' ] );
      exit;
    }

    $deleted = \factory\Campaigns::delete_campaigns( $ids );

    echo json_encode( [ 'success' => true, 'deleted' => $deleted ] );
    exit;
  }

  static public function delete_history_entry()
  {
    $history_id = \S::get( 'history_id' );

    if ( !$history_id )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie podano wpisu do usunięcia' ] );
      exit;
    }

    $result = \factory\Campaigns::delete_history_entry( $history_id );

    echo json_encode( [ 'success' => $result ? true : false ] );
    exit;
  }

  static public function delete_history_entries()
  {
    $ids = \S::get( 'ids' );

    if ( empty( $ids ) || !is_array( $ids ) )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie wybrano wpisow do usuniecia' ] );
      exit;
    }

    $ids = array_map( 'intval', $ids );
    $ids = array_filter( $ids, function( $id ) { return $id > 0; } );

    if ( empty( $ids ) )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie wybrano wpisow do usuniecia' ] );
      exit;
    }

    $deleted = \factory\Campaigns::delete_history_entries( $ids );

    echo json_encode( [ 'success' => true, 'deleted' => $deleted ] );
    exit;
  }

  static public function comment_add()
  {
    $campaign_id = (int) \S::get( 'campaign_id' );
    $comment = trim( \S::get( 'comment' ) );
    $date = \S::get( 'date' );

    if ( !$campaign_id || !$comment )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Brak wymaganych parametrów' ] );
      exit;
    }

    $result = \factory\Campaigns::add_campaign_comment( $campaign_id, $comment, $date ?: null );

    echo json_encode( [ 'success' => $result ? true : false ] );
    exit;
  }

  static public function comment_delete()
  {
    $comment_id = (int) \S::get( 'comment_id' );

    if ( !$comment_id )
    {
      echo json_encode( [ 'success' => false, 'message' => 'Nie podano komentarza' ] );
      exit;
    }

    $result = \factory\Campaigns::delete_campaign_comment( $comment_id );

    echo json_encode( [ 'success' => $result ? true : false ] );
    exit;
  }
}
