<commit_before>import React from 'react'
import Loading from '../Loading'
// import RestService from '../RestService'
import update from 'react-addons-update'
import MovieFolder from './MovieFolder'
import Switch from '../Switch'
import InputEditable from '../InputEditable'
import SimpleForm from '../SimpleForm'
import toastr from 'toastr'
import * as tb from 'tb-react'
import {configActions} from '../../actions/config'

let mb = 1024 * 1024

class SFConfigIndex extends React.Component {
  constructor(props, context) {
    super(props, context)

    this.providers = [
      {name: "opensubtitles", display_name: "Opensubtitles"},
      {name: "subscene", display_name: "Subscene"},
    ]

    this.state = {data: null, formData: {}, loading: false}
  }

  componentWillMount() {
    this.loadData()
  }

  async loadData() {
    // this.setState({loading: true})
    //
    // let data = await RestService.load("config")
    //
    // this.setState({data: data, loading: false})
  }

  async addFolder(data) {
    console.log('data', data);
    this.props.onPushField('src', data.src)
  }

  async addLanguage(data) {
    return this.updateConfig({
      'lang-$push': data.name
    })
  }

  async updateConfig(params) {
    // this.setState({loading: true})
    // try {
    //   let ret = await RestService.load("config/update", params)
    //   // console.log(ret);
    //
    //   this.setState({data: ret, loading: false})
    //
    //   return true
    // } catch (response) {
    //   // console.log(response);
    //   toastr.error(response.responseJSON.message)
    //
    //   this.setState({loading: false})
    //
    //   return false
    // }
  }

  removeFolder(src) {
    // console.log('remove ', src);
    // this.updateConfig({'src-$remove': src})
    this.props.removeField('src', src)
  }

  removeLang(src) {
    // console.log('remove ', src);
    this.updateConfig({'lang-$remove': src})
  }

  async updateProvider(checked, name) {
    // console.log(`update ${name} to ${checked}`);
    let fieldName = checked ? 'providers-$push' : 'providers-$remove'
    this.updateConfig({[fieldName]: name})
  }

  updateSwitchField(fieldName, checked) {
    this.updateConfig({[fieldName]: checked})
  }

  async onMinMovieSizeUpdate(value) {
    await this.updateConfig({'min-movie-size': value * mb})
  }

  async onMaxSubUpdate(value) {
    await this.updateConfig({'max-sub': value})
  }

  back() {
    this.context.router.goBack()
  }

  render() {
    // console.log('render', this.state.formData);
    let loading
    // if (this.state.loading) {
    //   loading = (
    //     <Loading />
    //   )
    // }

    const {
      config: {src: folders, lang, providers, force, remove},
      onReload,
    } = this.props
    // console.log('config', this.props.config);
    const config = this.props.config

    let content = (
      <div>
        <div className="row">
          <div className="col-sm-3"><strong>Movie Folders</strong></div>
          <div className="col-sm-9">
            {folders.map((folder, k) => {
              return (
                <MovieFolder
                  src={folder}
                  key={k}
                >
                  <tb.RemoteButton
                    name="Remove"
                    icon="trash"
                    hideName
                    type="danger"
                    url="Config/update"
                    params={{"src-$remove": folder}}
                    onComplete={onReload}
                    />
                </MovieFolder>
              )
            })}
            <SimpleForm
              url="Config/update"
              field={`src-$push`}
              onComplete={onReload}
            />
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Languages</strong></div>
          <div className="col-sm-9">
            {lang.map((item, k) => {
              return (
                <MovieFolder
                  src={item}
                  key={k}
                >
                  <tb.RemoteButton
                    name="Remove"
                    icon="trash"
                    hideName
                    type="danger"
                    url="Config/update"
                    params={{"lang-$remove": item}}
                    onComplete={onReload}
                    />
                </MovieFolder>
              )
            })}

            <SimpleForm
              url="Config/update"
              field={`lang-$push`}
              onComplete={onReload}
            />
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Providers</strong></div>
          <div className="col-sm-9">
            {this.providers.map((item, k) => {
              let tmp = providers.filter(x => x == item.name)
              let value = tmp.length == 1 ? true : false
              // console.log(item.display_name, value);
              return (
                <div key={k}>
                  <div className="col-sm-3">{item.display_name}</div>
                  <div className="col-sm-9">
                    <Switch checked={value} onChange={(checked) => this.updateProvider(checked, item.name)}/>
                  </div>
                </div>
              )
            })}
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Force download subtitle</strong></div>
          <div className="col-sm-9">
            <Switch checked={force}
              onChange={(checked) => this.updateSwitchField('force', checked)}/>
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3">
            <strong>Remove old subtitles if not found new subtitle</strong>
          </div>
          <div className="col-sm-9">
            <Switch checked={remove}
              onChange={(checked) => this.updateSwitchField('remove', checked)}/>
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3">
            <strong>Min movie size (MB)</strong><commit_msg>use APIAction button<commit_after>import React from 'react'
import Loading from '../Loading'
// import RestService from '../RestService'
import update from 'react-addons-update'
import MovieFolder from './MovieFolder'
import Switch from '../Switch'
import InputEditable from '../InputEditable'
import SimpleForm from '../SimpleForm'
import toastr from 'toastr'
import * as tb from 'tb-react'
import {configActions} from '../../actions/config'

let mb = 1024 * 1024

class SFConfigIndex extends React.Component {
  constructor(props, context) {
    super(props, context)

    this.providers = [
      {name: "opensubtitles", display_name: "Opensubtitles"},
      {name: "subscene", display_name: "Subscene"},
    ]

    this.state = {data: null, formData: {}, loading: false}
  }

  componentWillMount() {
    this.loadData()
  }

  async loadData() {
    // this.setState({loading: true})
    //
    // let data = await RestService.load("config")
    //
    // this.setState({data: data, loading: false})
  }

  async addFolder(data) {
    console.log('data', data);
    this.props.onPushField('src', data.src)
  }

  async addLanguage(data) {
    return this.updateConfig({
      'lang-$push': data.name
    })
  }

  async updateConfig(params) {
    // this.setState({loading: true})
    // try {
    //   let ret = await RestService.load("config/update", params)
    //   // console.log(ret);
    //
    //   this.setState({data: ret, loading: false})
    //
    //   return true
    // } catch (response) {
    //   // console.log(response);
    //   toastr.error(response.responseJSON.message)
    //
    //   this.setState({loading: false})
    //
    //   return false
    // }
  }

  removeFolder(src) {
    // console.log('remove ', src);
    // this.updateConfig({'src-$remove': src})
    this.props.removeField('src', src)
  }

  removeLang(src) {
    // console.log('remove ', src);
    this.updateConfig({'lang-$remove': src})
  }

  async updateProvider(checked, name) {
    // console.log(`update ${name} to ${checked}`);
    let fieldName = checked ? 'providers-$push' : 'providers-$remove'
    this.updateConfig({[fieldName]: name})
  }

  updateSwitchField(fieldName, checked) {
    this.updateConfig({[fieldName]: checked})
  }

  async onMinMovieSizeUpdate(value) {
    await this.updateConfig({'min-movie-size': value * mb})
  }

  async onMaxSubUpdate(value) {
    await this.updateConfig({'max-sub': value})
  }

  back() {
    this.context.router.goBack()
  }

  render() {
    // console.log('render', this.state.formData);
    let loading
    // if (this.state.loading) {
    //   loading = (
    //     <Loading />
    //   )
    // }

    const {
      config: {src: folders, lang, providers, force, remove},
      onReload,
    } = this.props
    // console.log('config', this.props.config);
    const config = this.props.config

    let content = (
      <div>
        <div className="row">
          <div className="col-sm-3"><strong>Movie Folders</strong></div>
          <div className="col-sm-9">
            {folders.map((folder, k) => {
              return (
                <MovieFolder
                  src={folder}
                  key={k}
                >
                  <tb.APIActionButton
                    name="Remove"
                    icon="trash"
                    hideName
                    type="danger"

                    action={[configActions.removeField, 'src', folder]}
                  />
                </MovieFolder>
              )
            })}
            <SimpleForm
              url="Config/update"
              field={`src-$push`}
              onComplete={onReload}
            />
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Languages</strong></div>
          <div className="col-sm-9">
            {lang.map((item, k) => {
              return (
                <MovieFolder
                  src={item}
                  key={k}
                >
                  <tb.APIActionButton
                    name="Remove"
                    icon="trash"
                    hideName
                    type="danger"
                    action={[configActions.removeField, 'lang', item]}
                  />
                </MovieFolder>
              )
            })}

            <SimpleForm
              url="Config/update"
              field={`lang-$push`}
              onComplete={onReload}
            />
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Providers</strong></div>
          <div className="col-sm-9">
            {this.providers.map((item, k) => {
              let tmp = providers.filter(x => x === item.name)
              let value = tmp.length === 1 ? true : false
              // console.log(item.display_name, value);
              return (
                <div key={k}>
                  <div className="col-sm-3">{item.display_name}</div>
                  <div className="col-sm-9">
                    <Switch checked={value} onChange={(checked) => this.updateProvider(checked, item.name)}/>
                  </div>
                </div>
              )
            })}
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3"><strong>Force download subtitle</strong></div>
          <div className="col-sm-9">
            <Switch checked={force}
              onChange={(checked) => this.updateSwitchField('force', checked)}/>
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3">
            <strong>Remove old subtitles if not found new subtitle</strong>
          </div>
          <div className="col-sm-9">
            <Switch checked={remove}
              onChange={(checked) => this.updateSwitchField('remove', checked)}/>
          </div>
        </div>
        <div className="row">
          <div className="col-sm-3">
            <strong>Min movie size (MB)</strong>
{'fraction_non_alphanumeric': 0.15229932421295533, 'fraction_numerical': 0.003626174386022746, 'mean_word_length': 1.6380434782608695, 'pattern_counts': {'":': 2, '<': 129, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7645951', 'n_tokens_mistral': 3538, 'n_tokens_neox': 3419, 'n_words': 663}