             return false
     return true
 
-
 class ProblemCommentsLists extends React.Component
     render: () ->
         <div>


ConnectedProblemCommentsLists = ConnectedComponent(ProblemCommentsLists, listOptions)

class SubmitWithActions extends React.Component
    constructor: (props) ->
        super(props)
        @state =
            commentText: ""
            bestSubmits: false
        @accept = @accept.bind this
        @ignore = @ignore.bind this
        @disqualify = @disqualify.bind this
        @comment = @comment.bind this
        @setField = @setField.bind this
        @setComment = @setComment.bind this
        @setQuality = @setQuality.bind this
        @toggleBestSubmits = @toggleBestSubmits.bind this
        @downloadSubmits = @downloadSubmits.bind this
        @copyTest = @copyTest.bind this

    componentDidUpdate: (prevProps, prevState) ->
        if !submitListEquals(prevProps.submits, @props.submits)
            @setState
                commentText: ""
                bestSubmits: false

    copyTest: (result) ->
        (e) =>
            e.stopPropagation()
            newComment = @state.commentText
            if newComment.length
                newComment += "\n\n"
            newComment += "Входные данные:\n#{result.input.trim()}\n\nВывод программы:\n#{result.output.trim()}\n\nПравильный ответ:\n#{result.corr.trim()}"
            @setState
                commentText: newComment 
            return false

    setResult: (result) ->
        @syncSetOutcome(result)
        @props.handleDone?()

    syncSetOutcome: (result) ->
        await callApi "setOutcome/#{@props.currentSubmit._id}", {
            result,
            comment: @state.commentText
        }
        @props.syncHandleDone?()
        @props.handleReload()

    downloadSubmits: () ->
        await callApi "downloadSubmitsForUserAndProblem/#{@props.user._id}/#{@props.result.fullTable._id}"
        @props.handleReload()

    accept: () ->
        @setResult("AC")

    ignore: () ->
        @setResult("IG")

    disqualify: () ->
        @setResult("DQ")

    comment: () ->
        @setResult(null)

    setQuality: (quality) ->
        () =>
            await callApi "setQuality/#{@props.currentSubmit._id}/#{quality}", {}
            @props.handleReload()

    toggleBestSubmits: (e) ->
        if e
            e.preventDefault()
        @setState
            bestSubmits: not @state.bestSubmits

    setComment: (comment) ->
        () =>
            newText = entities.decode(comment.text)
            newText = newText.trim()
            for tail in TAIL_TEXTS
                if newText.endsWith(tail)
                    newText = newText.substring(0, newText.length - tail.length);
            newText = newText.trim()
            if @state.commentText.length
                newText = @state.commentText + "\n\n" + newText
            @setField("commentText", newText)

    setField: (field, value) ->
        newState = {@state...}
        newState[field] = value
        @setState(newState)

    render: () ->
        admin = @props.me?.admin
        <div>
            <Submit submit={@props.currentSubmit} showHeader me={@props.me} copyTest={@copyTest}/>
            {(not @props.currentSubmit.similar) && (not @props.result.findMistake) && <div>
                <div>
                    {
                    if @props.currentSubmit.outcome in ["OK", "AC"]
                        starsClass = styles.stars
                    else
                        starsClass = ""
                    <div className={starsClass}>
                        <FontAwesome name="times" key={0} onClick={@setQuality(0)}/>
                        {(<FontAwesome
                            name={"star" + (if x <= @props.currentSubmit.quality then "" else "-o")}
                            key={x}
                            onClick={@setQuality(x)}/> \
                            for x in [1..5])}
                    </div>
                    }
                </div>
                {
                if @props.bestSubmits?.length
                    <span>
                        <a href="#" onClick={@toggleBestSubmits}>Хорошие решения</a>
                        {" = " + @props.bestSubmits.length}
                        {" " + @props.bestSubmits.map((submit) -> submit.language || "unknown").join(", ")}
                    </span>
                }
                <FieldGroup
                        id="commentText"
                        label="Комментарий"
                        componentClass="textarea"
                        setField={@setField}
                        style={{ height: 200 }}
                        state={@state}/>
                <FormGroup>
                    {
                    bsSize = null
                    bsCommentSize = null
                    if not (@props.currentSubmit.outcome in ["OK", "AC", "IG"])
                        bsSize = "xsmall"
                    if not @props.result?.activated
                        bsSize = "xsmall"
                        bsCommentSize = "xsmall"
                    <ButtonGroup>
                        <Button onClick={@accept} bsStyle="success" bsSize={bsSize}>Зачесть</Button>
                        <Button onClick={@ignore} bsStyle="info" bsSize={bsSize}>Проигнорировать</Button>
                        <Button onClick={@comment}  bsSize={bsCommentSize}>Прокомментировать</Button>
                        <Button onClick={@disqualify} bsStyle="danger" bsSize="xsmall">Дисквалифицировать</Button>
                    </ButtonGroup>
                    }
                </FormGroup>
                <Button onClick={@downloadSubmits} bsSize="xsmall">re-download submits</Button>
                {
                admin and @props.currentSubmit and not @props.currentSubmit.similar and <Col xs={12} sm={12} md={12} lg={12}>
                    <ConnectedProblemCommentsLists problemId={@props.result.fullTable._id} handleCommentClicked={@setComment}/>
                </Col>
                }
                {
                admin && @state.bestSubmits && <BestSubmits submits={@props.bestSubmits} close={@toggleBestSubmits} stars/>
                }
            </div>}
            {@props.result.findMistake && <Link to="/findMistake/#{@props.result.findMistake}">Исходный поиск ошибки</Link>}
        </div>

export class SubmitListWithDiff extends React.Component

        @state =
            currentSubmit: if @props.submits then @props.submits[@props.submits.length - 1] else null
            currentDiff: [undefined, undefined]
        props.setCurrentSubmit?(@state.currentSubmit)
        @setCurrentSubmit = @setCurrentSubmit.bind this
        @setCurrentDiff = @setCurrentDiff.bind this

