/*
 * Copyright 2013-2026, Seqera Labs
 *
 * 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.
 */

package nextflow.extension

import nextflow.Channel
import spock.lang.Retry
import spock.lang.Specification

import static test.ScriptHelper.runDataflow
/**
 *
 * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
 */
class DataflowMathExtensionTest extends Specification {

    Comparator makeComparator(Closure c) { c as Comparator }

    def 'should return the min value'() {

        when:
        def result = runDataflow {
            Channel.of(4,1,7,5).min()
        }
        then:
        result.val == 1

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").min { it.size() }
        }
        then:
        result.val == "hi"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").min { a,b -> a.size()<=>b.size() }
        }
        then:
        result.val == "hi"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").min { a,b -> a.size()<=>b.size() }
        }
        then:
        result.val == "hi"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").min ( makeComparator({ a,b -> a.size()<=>b.size() }) )
        }
        then:
        result.val == "hi"

    }

    def 'should return the max value'() {

        when:
        def result = runDataflow {
            Channel.of(4,1,7,5).max()
        }
        then:
        result.val == 7

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").max { it.size() }
        }
        then:
        result.val == "hello"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").max { a,b -> a.size()<=>b.size() }
        }
        then:
        result.val == "hello"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").max { a,b -> a.size()<=>b.size() }
        }
        then:
        result.val == "hello"

        when:
        result = runDataflow {
            Channel.of("hello","hi","hey").max (makeComparator({ a,b -> a.size()<=>b.size() }))
        }
        then:
        result.val == "hello"
    }

    def 'should return the sum'() {

        when:
        def result = runDataflow {
            Channel.of(4,1,7,5).sum()
        }
        then:
        result.val == 17

        when:
        result = runDataflow {
            Channel.of(4,1,7,5).sum { it * 2 }
        }
        then:
        result.val == 34

        when:
        result = runDataflow {
            Channel.of( [1,1,1], [0,1,2], [10,20,30] ). sum()
        }
        then:
        result.val == [ 11, 22, 33 ]
    }


    def 'should return the mean'() {

        when:
        def result = runDataflow {
            Channel.of(10,20,30).mean()
        }
        then:
        result.val == 20

        when:
        result = runDataflow {
            Channel.of(10,20,30).mean { it * 2 }
        }
        then:
        result.val == 40

        when:
        result = runDataflow {
            Channel.of( [10,20,30], [10, 10, 10 ], [10, 30, 50]).mean()
        }
        then:
        result.val == [10, 20, 30]
    }

    def 'should convert string to integers' () {

        when:
        def result = runDataflow {
            Channel.value('11').toInteger()
        }
        then:
        result.val == 11

        when:
        def list = runDataflow {
            Channel.of('1', '4\n', ' 7 ', '100' )
                .toInteger()
                .toList()
        }.getVal()

        then:
        list.size() == 4
        list[0] == 1
        list[1] == 4
        list[2] == 7
        list[3] == 100
        list[0] instanceof Integer
        list[1] instanceof Integer
        list[2] instanceof Integer
        list[3] instanceof Integer
    }


    def 'should convert string to long' () {

        when:
        def result = runDataflow {
            Channel.value('33').toLong()
        }
        then:
        result.val == 33L

        when:
        def list = runDataflow {
            Channel.of('1', '4\n', ' 7 ', '100' )
                .toLong()
                .toList()
        }.getVal()

        then:
        list.size() == 4
        list[0] == 1
        list[1] == 4
        list[2] == 7
        list[3] == 100
        list[0] instanceof Long
        list[1] instanceof Long
        list[2] instanceof Long
        list[3] instanceof Long
    }

    def 'should convert string to float' () {

        when:
        def result = runDataflow {
            Channel.value('99.1').toFloat()
        }
        then:
        result.val == 99.1f

        when:
        def list = runDataflow {
            Channel.of('1', '4\n', ' 7.5 ', '100.1' )
                .toFloat()
                .toList()
        }.getVal()

        then:
        list.size() == 4
        list[0] == 1
        list[1] == 4
        list[2] == 7.5f
        list[3] == 100.1f
        list[0] instanceof Float
        list[1] instanceof Float
        list[2] instanceof Float
        list[3] instanceof Float
    }

    def 'should convert string to double' () {

        when:
        def result = runDataflow {
            Channel.value('99.1').toDouble()
        }
        then:
        result.val == 99.1d

        when:
        def list = runDataflow {
            Channel.of('1', '4\n', ' 7.5 ', '100.1' )
                .toDouble()
                .toList()
        }.getVal()

        then:
        list.size() == 4
        list[0] == 1
        list[1] == 4
        list[2] == 7.5d
        list[3] == 100.1d
        list[0] instanceof Double
        list[1] instanceof Double
        list[2] instanceof Double
        list[3] instanceof Double

    }

    @Retry
    def 'should return a random sample' () {

        when:
        def result = runDataflow {
            Channel.of(0,1,2,3,4,5,6,7,8,9).randomSample(5).toList()
        }.val as List

        then:
        result.size() == 5
        result.unique().size() == 5
        result != [0,1,2,3,4]
        result[0] in 0..9
        result[1] in 0..9
        result[2] in 0..9
        result[3] in 0..9
        result[4] in 0..9

    }

}
