import { segmentsIntr } from './util'
import { color } from 'echarts'

export default function (publicProps) {
    let { derrickConfig, derrickSize, partSize, colors } = publicProps
    derrickConfig = {
        padLeft: 30,
        axisMax: 40,
        axisWidth: 10,
        axisData: { labels: [], strickLines: { left: [], right: [] } },
        echartsStricks: [],
        backTartans: [],
        sealBottomRect: [],

        lineWidth: 3
    }
    derrickSize = { width: 160, height: 280 }
    function partSizeInit () {
        return {
            topRed: { width: derrickSize.width * 0.1, height: 8 },
            topRedBottom: { start: [0, 0], end: [0, 0], width: 0 },
            axisLeft: { start: [0, 0], end: [0, 0] },
            axisRight: { start: [0, 0], end: [0, 0] },
            sealBottom: { width: derrickSize.width * 0.5, height: 15 }
        }
    }
    partSize = partSizeInit()

    function derrickCal () {
        partSize = partSizeInit()
        derrickConfig.axisData = { labels: [], strickLines: { left: [], right: [] } }
        partSize.topRedBottom.width = partSize.topRed.width * 5
        partSize.topRedBottom.start = [
            (derrickSize.width - partSize.topRedBottom.width) / 2,
            0
        ]
        partSize.topRedBottom.end = [
            partSize.topRedBottom.start[0] + partSize.topRedBottom.width,
            0
        ]
        partSize.axisLeft.start = [
            (derrickSize.width - partSize.topRedBottom.width) / 2 + partSize.topRed.width,
            0
        ]
        partSize.axisLeft.end = [0 + derrickConfig.axisWidth, derrickSize.height]
        partSize.axisRight.start = [
            (derrickSize.width - partSize.topRedBottom.width) / 2 + partSize.topRed.width * 4,
            0
        ]
        partSize.axisRight.end = [derrickSize.width - derrickConfig.axisWidth, derrickSize.height]

        // short Stricks
        const bigInterval = derrickSize.height / (derrickConfig.axisMax / 10)
        const smallInterval = bigInterval / 5
        for (var i = derrickConfig.axisMax; i >= 0; i--) {
            if (i % 10 === 0 && i !== 0) {
                derrickConfig.axisData.labels.push(i)
            }
        }
        for (var i2 = 0; i2 <= derrickConfig.axisMax; i2++) {
            if (i2 % 2 === 0) {
                const pointY = i2 / 2 * smallInterval
                const xLeft = [0, pointY]
                const xRight = [derrickSize.width, pointY]
                const params = {
                    a: { x: partSize.axisLeft.start[0], y: partSize.axisLeft.start[1] },
                    b: { x: partSize.axisLeft.end[0], y: partSize.axisLeft.end[1] },
                    c: { x: xLeft[0], y: xLeft[1] },
                    d: { x: xRight[0], y: xRight[1] }
                }
                const crossPoint = segmentsIntr(params)
                derrickConfig.axisData.strickLines.left.push([crossPoint, [crossPoint[0] - derrickConfig.axisWidth, crossPoint[1]]])

                const rightParams = {
                    a: { x: partSize.axisRight.start[0], y: partSize.axisRight.start[1] },
                    b: { x: partSize.axisRight.end[0], y: partSize.axisRight.end[1] },
                    c: { x: xLeft[0], y: xLeft[1] },
                    d: { x: xRight[0], y: xRight[1] }
                }
                const rightCrossPoint = segmentsIntr(rightParams)
                derrickConfig.axisData.strickLines.right.push([rightCrossPoint, [rightCrossPoint[0] + derrickConfig.axisWidth, rightCrossPoint[1]]])
            }
        }
        derrickConfig.echartsStricks = derrickConfig.axisData.strickLines.left.map((p, i) => {
            if (i % 5 === 0 && i !== 0) {
                return { coords: p, lineStyle: { width: derrickConfig.lineWidth, color: 'red' } }
            } else {
                return { coords: p, lineStyle: { width: 1, color: colors.rule } }
            }
        }).concat(derrickConfig.axisData.strickLines.right.map((p, i) => {
            if (i % 5 === 0 && i !== 0) {
                return { coords: p, lineStyle: { width: derrickConfig.lineWidth, color: 'red' } }
            } else {
                return { coords: p, lineStyle: { width: 1, color: colors.rule } }
            }
        }))

        // back tartans
        const leftInter = []
        const rightInter = []
        for (var i3 = 0; i3 <= derrickConfig.axisMax; i3++) {
            if (i3 % 10 === 0) {
                leftInter.push(derrickConfig.axisData.strickLines.left[i3 / 2][0])
                rightInter.push(derrickConfig.axisData.strickLines.right[i3 / 2][0])
            }
        }
        const leftInterLine = { coords: [], lineStyle: { width: 3, color: colors.tartan } }
        const rightInterLine = { coords: [], lineStyle: { width: 3, color: colors.tartan } }
        for (var i4 = 0; i4 < leftInter.length; i4++) {
            if (i4 % 2 === 0) {
                leftInterLine.coords.push(leftInter[i4])
                rightInterLine.coords.push(rightInter[i4])
            } else {
                leftInterLine.coords.push(rightInter[i4])
                rightInterLine.coords.push(leftInter[i4])
            }
        }
        derrickConfig.backTartans.push(leftInterLine)
        derrickConfig.backTartans.push(rightInterLine)

        // sealBottom Rect
        const sealPolyline = { coords: [], lineStyle: { opacity: 0.8 } };
        (function () {
            const leftTop = []
            const leftBottom = []
            const rightBottom = []
            const rightTop = []
            leftTop.push((derrickSize.width - partSize.sealBottom.width) / 2)
            leftTop.push(derrickSize.height - partSize.sealBottom.height)
            leftBottom.push(leftTop[0])
            leftBottom.push(derrickSize.height)
            rightBottom.push(leftTop[0] + partSize.sealBottom.width)
            rightBottom.push(derrickSize.height)
            rightTop.push(rightBottom[0])
            rightTop.push(leftTop[1])
            sealPolyline.coords = [leftTop, leftBottom, rightBottom, rightTop, leftTop]
            derrickConfig.sealBottomRect.push(sealPolyline)
        }())
    }
    derrickCal()

    return {
        derrickConfig,
        partSize,
        derrickSize,

        derrickCal
    }
}
