<commit_before>/*
Copyright 2016 First People's Cultural Council

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Immutable from 'immutable'

import HangManGame from './hangman'

// REDUX
import { connect } from 'react-redux'
// REDUX: actions/dispatch/func
import { fetchCharacters } from 'providers/redux/reducers/fvCharacter'
import { fetchWords } from 'providers/redux/reducers/fvWord'

import selectn from 'selectn'

import PromiseWrapper from 'views/components/Document/PromiseWrapper'

import ProviderHelpers from 'common/ProviderHelpers'
import StringHelpers from 'common/StringHelpers'
import NavigationHelpers from 'common/NavigationHelpers'

const PUZZLES = 25
const MIN_REQ_WORDS = 5

const { func, object } = PropTypes
export class Hangman extends Component {
  static propTypes = {
    routeParams: object.isRequired,
    // REDUX: reducers/state
    computeCharacters: object.isRequired,
    computeWords: object.isRequired,
    // REDUX: actions/dispatch/func
    fetchCharacters: func.isRequired,
    fetchWords: func.isRequired,
  }

  constructor(props, context) {
    super(props, context)

    this.state = {
      currentPuzzleIndex: 0,
    }

    this.newPuzzle = this.newPuzzle.bind(this)
  }

  componentDidMount() {
    this.fetchData(this.props)
  }

  fetchData(props /*, pageIndex, pageSize, sortOrder, sortBy*/) {

    props.fetchCharacters(
      props.routeParams.dialect_path + '/Alphabet',
      '&currentPageIndex=0' + '&pageSize=100' + '&sortOrder=asc' + '&sortBy=fvcharacter:alphabet_order'
    )

    props.fetchWords(
      props.routeParams.dialect_path + '/Dictionary',
      //' AND ' + ProviderHelpers.switchWorkspaceSectionKeys('fv:related_pictures', this.props.routeParams.area) +'/* IS NOT NULL' +
      ' AND fv:available_in_childrens_archive = 1' +
        ' AND ' +
        ProviderHelpers.switchWorkspaceSectionKeys('fv:related_audio', this.props.routeParams.area) +
        '/* IS NOT NULL' +
        //' AND fv-word:available_in_games = 1' +
        '&currentPageIndex=' +
        StringHelpers.randomIntBetween(0, 10) +
        '&pageSize=' +
        PUZZLES
    )

    
  }

  newPuzzle() {
    const computeWords = ProviderHelpers.getEntry(
      this.props.computeWords,
      this.props.routeParams.dialect_path + '/Dictionary'
    )

    if (
      this.state.currentPuzzleIndex < PUZZLES &&
      this.state.currentPuzzleIndex < selectn('response.resultsCount', computeWords) - 1
    ) {
      this.setState({
        currentPuzzleIndex: this.state.currentPuzzleIndex + 1,
      })
    } else {
      this.fetchData(this.props)
      this.setState({
        currentPuzzleIndex: 0,
      })
    }
  }

  /**
   * Render
   */
  render() {<commit_msg>FW-1058 (#212)<commit_after>/*
Copyright 2016 First People's Cultural Council

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Immutable from 'immutable'

import HangManGame from './hangman'

// REDUX
import { connect } from 'react-redux'
// REDUX: actions/dispatch/func
import { fetchCharacters } from 'providers/redux/reducers/fvCharacter'
import { fetchWords } from 'providers/redux/reducers/fvWord'

import selectn from 'selectn'

import PromiseWrapper from 'views/components/Document/PromiseWrapper'

import ProviderHelpers from 'common/ProviderHelpers'
import StringHelpers from 'common/StringHelpers'
import NavigationHelpers from 'common/NavigationHelpers'

const PUZZLES = 25
const MIN_REQ_WORDS = 5

const { func, object } = PropTypes
export class Hangman extends Component {
  static propTypes = {
    routeParams: object.isRequired,
    // REDUX: reducers/state
    computeCharacters: object.isRequired,
    computeWords: object.isRequired,
    // REDUX: actions/dispatch/func
    fetchCharacters: func.isRequired,
    fetchWords: func.isRequired,
  }

  constructor(props, context) {
    super(props, context)

    this.state = {
      currentPuzzleIndex: 0,
    }

    this.newPuzzle = this.newPuzzle.bind(this)
  }

  componentDidMount() {
    this.fetchData(this.props)
    
  }

  //TO DO: Improve the fetch data such that the PageIndex can no longer be the same twice in a row if there are multiple possiblities
  async fetchData(props /*, pageIndex, pageSize, sortOrder, sortBy*/) {
    await props.fetchCharacters(
      props.routeParams.dialect_path + '/Alphabet',
      '&currentPageIndex=0' + '&pageSize=100' + '&sortOrder=asc' + '&sortBy=fvcharacter:alphabet_order'
    )
      //First we make a guess about the word results
    await props.fetchWords(
      props.routeParams.dialect_path + '/Dictionary',
      //' AND ' + ProviderHelpers.switchWorkspaceSectionKeys('fv:related_pictures', this.props.routeParams.area) +'/* IS NOT NULL' +
      ' AND fv:available_in_childrens_archive = 1' +
        ' AND ' +
        ProviderHelpers.switchWorkspaceSectionKeys('fv:related_audio', this.props.routeParams.area) +
        '/* IS NOT NULL' +
        //' AND fv-word:available_in_games = 1' +
        '&currentPageIndex=' +
        //currentPageIndex +
        StringHelpers.randomIntBetween(0, Math.ceil((this.state.resultCount / PUZZLES)) || 10)+
        '&pageSize=' +
        PUZZLES
    )
      
      const queryResult = ProviderHelpers.getEntry(this.props.computeWords,
        this.props.routeParams.dialect_path + '/Dictionary')

      let resultCount = selectn(['response', 'resultsCount'], queryResult)
      
      if(10 > (resultCount / PUZZLES) && this.state.resultCount == undefined){
        //We refine our intial query/guess based on the first one
        //This should only happen once
        this.setState({resultCount: resultCount})
        await props.fetchWords(
          props.routeParams.dialect_path + '/Dictionary',
          //' AND ' + ProviderHelpers.switchWorkspaceSectionKeys('fv:related_pictures', this.props.routeParams.area) +'/* IS NOT NULL' +
          ' AND fv:available_in_childrens_archive = 1' +
            ' AND ' +
            ProviderHelpers.switchWorkspaceSectionKeys('fv:related_audio', this.props.routeParams.area) +
            '/* IS NOT NULL' +
            //' AND fv-word:available_in_games = 1' +
            '&currentPageIndex=' +
            //currentPageIndex +
            StringHelpers.randomIntBetween(0, Math.ceil((resultCount / PUZZLES))) +
            '&pageSize=' +
            PUZZLES
        )
      }
      //Will trigger re-render
  }

  newPuzzle() {
    const computeWords = ProviderHelpers.getEntry(
      this.props.computeWords,
      this.props.routeParams.dialect_path + '/Dictionary'
    )

    if (this.state.currentPuzzleIndex < (this.state.resultCount % PUZZLES) - 1) {
      this.setState({
        currentPuzzleIndex: this.state.currentPuzzleIndex + 1,
      })
      //This is for when we could potentially run out of words, but we are not sure
    } else if(this.state.currentPuzzleIndex < (PUZZLES - 1) ) {
      if(this.state.resultCount < PUZZLES * (selectn('response.pageIndex', computeWords) + 1)){
        this.fetchData(this.props)
        this.setState({
          currentPuzzleIndex: 0,
        })
      } else {
        this.setState({
          currentPuzzleIndex: this.state.currentPuzzleIndex + 1,
        })
      }
    } else {
      this.fetchData(this.props)
      this.setState({
        currentPuzzleIndex: 0,
      })
      //,await () => {this.fetchData(this.props)}
      //, () => {} Add fetchData here if puzzle index needs to be updated first 
    }
  }

  /**
   * Render
   */
  render() {
{'fraction_non_alphanumeric': 0.10626607435118074, 'fraction_numerical': 0.007481879822305354, 'mean_word_length': 3.0972222222222223, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4558811', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2516, 'n_words': 634}