<?php

class lessonList {
  public $flat = array();

  public function __construct($blogId = null) {
    $c = new Criteria();
    $tbllessons = TbllessonPeer::doSelect($c);

    foreach ($tbllessons as $tbllesson) {
      $id = $tbllesson->getLesId();
      $cat = $tbllesson->getLesCategory();
      if (!isset($this->flat[$cat])) $this->flat[$cat] = array();
      $this->flat[$cat][$id] = $tbllesson->getLesTitle();
    }
    foreach ($this->flat as &$items) {ksort($items); $items[-1] = 'Ngoài chương trình SGK';}
  }

  public function getCatList($cat_id) {
    return @$this->flat[$cat_id];
  }

  public function getTitle($cat_id, $les_id) {
    if (is_string($les_id) && preg_match('/\d+_\d+/', $les_id)) {
      $data = explode('_', $les_id);
      $cat = intval($data[0]);
      $les = intval($data[1]);
      return commonData::$baseCatTree->getCatItem($cat)->name.'. '.$this->flat[$cat][$les];
    }
    return $this->flat[$cat_id][$les_id];
  }

  public function decodeTitle($title, $extra = '') {
    $title = trim(strtolower(myUtility::vietDecode($title)));
    for ($i = 0; $i < 2; $i++) $title = preg_replace('@^(tuan|chuong|bai|tiet'.$extra.') *[\divx]+\W*@', '', $title);
    return $title;
  }

  public function getRecursiveLessonList($cat_id) {
    $list = @$this->flat[$cat_id];
    if ($list == null) {
      $list = array();
      $catItem = commonData::$baseCatTree->getCatItem($cat_id);
      foreach ($catItem->items as $key => $item) {
        foreach (@$this->flat[$key] as $sub => $lesson) $list[$key.'_'.$sub] = $lesson;
        if (!@$this->flat[$key]) $list += $this->getRecursiveLessonList($key);
      }
    }
    return $list;
  }

  public function getSimilar($cat_id, $title) {
    $list = $this->getRecursiveLessonList($cat_id);
    $extra = '|toan|toan hoc|ngu van|van|van hoc|tieng anh|anh|vat ly|ly|hoa hoc|hoa|sinh hoc|sinh|lich su|su|dia ly|dia|gdcd|am nhac|my thuat|the duc|cong nghe|tin hoc|tin|vat li|li|dia li|mi thuat|dai so|hinh hoc';
    $title = $this->decodeTitle(str_replace('_', ' ', $title), $extra);
    $title_word = preg_split('/\W/', $title, -1, PREG_SPLIT_NO_EMPTY);
    $title = preg_replace('/\W/', '', $title);
    $max = 0;
    $maxc = 0;
    $id = null;
    foreach ($list as $key => &$lesson) {
      $lesson = $this->decodeTitle($lesson);
      if ($lesson == $title) {$id = $key; break;}
      $lesson_word = preg_split('/\W/', $lesson, -1, PREG_SPLIT_NO_EMPTY);
      $lesson = preg_replace('/\W/', '', $lesson);
      if (strlen($title) < strlen($lesson)*2/10 || strlen($lesson) < strlen($title)*2/10) continue;

      $sublen_char = $this->commonSubStrLength($lesson, $title);
      $sublen_word = $this->commonSubStrLength($lesson_word, $title_word);
      $same_char = $sublen_char > strlen($title)*7/10 || $sublen_char > strlen($lesson)*7/10;
      $same_word = $sublen_word > count($title_word)*5/10 || $sublen_word > count($lesson_word)*5/10;
      if (!$same_char || !$same_word) continue;

      if ($max < $sublen_word || $max == $sublen_word && ($maxc < $sublen_char || $maxc == $sublen_char && strlen($lesson) < strlen($list[$id]))) {
        $maxc = $sublen_char;
        $max = $sublen_word;
        $id = $key;
      }
    }
    return $id;
  }

  private function commonSubStrLength($s1, $s2) {
    $n1 = is_array($s1)? count($s1): strlen($s1);
    $n2 = is_array($s2)? count($s2): strlen($s2);
    $a = array_fill(0, $n1+1, array_fill(0, $n2+1, 0));

    for ($i = 1; $i <= $n1; $i++)
    for ($j = 1; $j <= $n2; $j++)
      if ($s1[$i-1] == $s2[$j-1]) $a[$i][$j] = $a[$i-1][$j-1] + 1;
      else $a[$i][$j] = max($a[$i-1][$j], $a[$i][$j-1]);

    return $a[$n1][$n2];
  }

  public function getJsDataFile($cat) {
    return sfConfig::get('sf_web_dir')."/opcode/lessons_$cat.js";
  }

  public function genJsDataFile() {
    foreach ($this->flat as $cat => $items) {
      $jsfile = $this->getJsDataFile($cat);
      if (!file_exists($jsfile)) {
        $data = "var lesson_list = [\n";
        foreach ($items as $title) $data .= "\"$title\",\n";
        $data = substr($data, 0, -2)."];\n";
        file_put_contents($jsfile, $data);
      }
    }
  }

  public function allowTag($cat_id) {
    $base = blogStore::$catTree->getCatItem($cat_id)->base;
    return in_array(blogStore::$blogId, array(12, 763773)) && (blogStore::canMod($cat_id) || commonData::$lessons->getCatList($base) || in_array($base, sfConfig::get('sf_tagging_folder_list')));
  }
}
