<?php

class Translation_Race extends GP_Plugin {
	var $id = 'translation-race';

	var $projects = array();
	var $sub_projects = array();

	function __construct() {
		parent::__construct();

		$this->add_action( 'pre_tmpl_load', array( 'args' => 2 ) );
		$this->add_filter( 'gp_url', array( 'args' => 3 ) );
		$this->add_action( 'project_template_translation_set_extra', array( 'args' => 2 ) );
		$this->add_action( 'gettext', array( 'args' => 3 ) );
	}

	function pre_tmpl_load( $template, $args ) {
		if ( isset( $args['projects'] ) ) {
			$this->projects = $args['projects'];
			wp_enqueue_style( 'translation-race', '/../plugins/translation-race.css' );
		}

		if ( isset( $args['sub_projects'] ) ) {
			$this->sub_projects = $args['sub_projects'];
			wp_enqueue_style( 'translation-race', '/../plugins/translation-race.css' );
		}

		if ( isset( $args['translation_sets'] ) ) {
			ob_start( array( &$this, 'set_obhandler' ) );
		}
	}

	function gp_url( $url, $path, $query ) {
		if ( is_array( $path ) && $path[0] == 'projects' && strpos( '/' . implode( '/', $path ), '/-' ) === false ) {
			global $gpdb;

			$project = current( $this->projects );
			next( $this->projects );
			if ( !$project )
				return $url;

			if ( false === $original_count = wp_cache_get( $project->id, 'translation_race_originals_count' ) ) {
				$original_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->originals}` WHERE `project_id` = %d AND `status` LIKE '+%%'", $project->id ) );
				wp_cache_add( $project->id, $original_count, 'translation_race_originals_count', 3600 );
			}
			if ( false === $set_count = wp_cache_get( $project->id, 'translation_race_set_count' ) ) {
				$set_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translation_sets}` WHERE `project_id` = %d", $project->id ) );
				wp_cache_add( $project->id, $set_count, 'translation_race_set_count', 3600 );
			}

			if ( false === $translated_count = wp_cache_get( $project->id, 'translation_race_translated_count' ) ) {
				$translated_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translations}` AS `t` LEFT JOIN `{$gpdb->translation_sets}` AS `ts` ON `translation_set_id` = `ts`.`id` LEFT JOIN `{$gpdb->originals}` AS `o` ON `original_id` = `o`.`id` WHERE `ts`.`project_id` = %d AND `t`.`status` = 'current' AND `o`.`status` LIKE '+%%'", $project->id ) );
				wp_cache_add( $project->id, $translated_count, 'translation_race_translated_count', 3600 );
			}
			if ( false === $fuzzy_count = wp_cache_get( $project->id, 'translation_race_fuzzy_count' ) ) {
				$fuzzy_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->translations}` LEFT JOIN `{$gpdb->translation_sets}` AS `ts` ON `translation_set_id` = `ts`.`id` WHERE `project_id` = %d AND `status` = 'fuzzy'", $project->id ) );
				wp_cache_add( $project->id, $fuzzy_count, 'translation_race_fuzzy_count', 3600 );
			}

			$percent_completed = $set_count && $original_count ? $translated_count / $set_count / $original_count * 100 : 0;
			$percent_translated = $set_count && $original_count ? ( $translated_count + $fuzzy_count ) / $set_count / $original_count * 100 : 0;

			echo '<span class="translation-race" title="', number_format( $translated_count ), ' of ', number_format( $original_count * $set_count ), ' strings translated"><span class="translation-race-progress-bar-fuzzy" style="width: ', $percent_translated, '%;"></span><span class="translation-race-progress-bar" style="width: ', $percent_completed, '%;"></span><span class="translation-race-percentage">', round( $percent_completed, 1 ), '%</span></span> ';
		}

		return $url;
	}

	function gettext( $a, $b, $c = 'default' ) {
		if ( $b == 'Sub-projects of %s:' && $c == 'default' ) {
			$this->projects = $this->sub_projects;
		}

		return $a;
	}

	function project_template_translation_set_extra( $set, $project ) {
		global $gpdb;

		if ( false === $original_count = wp_cache_get( $project->id, 'translation_race_originals_count' ) ) {
			$original_count = (int) $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM `{$gpdb->originals}` WHERE `project_id` = %d AND `status` LIKE '+%%'", $project->id ) );
			wp_cache_add( $project->id, $original_count, 'translation_race_originals_count', 3600 );
		}
		$translated_count = (int) $set->current_count;
		$fuzzy_count = (int) $set->fuzzy_count;

		$percent_completed = $original_count ? $translated_count / $original_count * 100 : 0;
		$percent_translated = $original_count ? ( $translated_count + $fuzzy_count ) / $original_count * 100 : 0;

		echo '<!--translation_race_wrongside--><span class="translation-race" title="', number_format( $translated_count ), ' of ', number_format( $original_count ), ' strings translated"><span class="translation-race-progress-bar-fuzzy" style="width: ', $percent_translated, '%;"></span><span class="translation-race-progress-bar" style="width: ', $percent_completed, '%;"></span><span class="translation-race-percentage">', round( $percent_completed, 1 ), '%</span></span><!--translation_race_endwrongside-->';
	}

	function set_obhandler( $buffer ) {
		return preg_replace( '/<li>([\s\S]*?)<!--translation_race_wrongside-->([\s\S]*?)<!--translation_race_endwrongside-->([\s\S]*?)<\/li>/', '<li>$2 $1 $3</li>', $buffer );
	}
}

GP::$plugins->translation_race = new Translation_Race();
