const acorn = require('acorn');
const walk = require("acorn-walk")
const qs = require('qs');
const cssom = require('cssom');
const commonStyle = require('../../extra/style');
const util = require('../../util/index')

const _reg = /(\boverflow\b)|(\bfontFamily\b)|(\bopacity\b)|(\bpadding\b)|(\bborderRadius\b)|(\bfont\b)|(\bborder\b)|(\bdirection\b)|(\bflex-direction\b)|(\bjustify-content\b)|(\balign-content\b)|(\balign-items\b)|(\balign-self\b)|(\bflex-wrap\b)|(\bdisplay\b)|(\bposition\b)|(\bflex\b)|(\bflex-grow\b)|(\bflex-shrink\b)|(\bflex-basis\b)|(\bleft\b)|(\btop\b)|(\bright\b)|(\bbottom\b)|(\bmargin\b)|(\bmargin-left\b)|(\bmargin-top\b)|(\bmargin-right\b)|(\bmargin-bottom\b)|(\bpadding-left\b)|(\bpadding-top\b)|(\bpadding-right\b)|(\bpadding-bottom\b)|(\bwidth\b)|(\bheight\b)|(\bmin-width\b)|(\bmin-height\b)|(\bmax-width\b)|(\bmax-height\b)|(\bborder-width\b)|(\bborder-color\b)|(\bborder-style\b)|(\btext-align\b)|(\bfont-size\b)|(\bfont-style\b)|(\btext-overflow\b)|(\bfont-weight\b)|(\bcolor\b)|(\bbackground-color\b)|(\bline-height\b)|(\btext-decoration\b)|(\bword-wrap\b)|(\bwhite-space\b)|(\bbox-sizing\b)/

module.exports = function (source) {
    let that = this;
    if (global._bili_cache_data.coverMapping[this.resourcePath]) {
        const ast = acorn.parse(source, {
            sourceType: 'module'
        });

        walk.simple(ast, {
            ArrayExpression(node) {
                let _styleText = node.elements[1].value;
                let _styleAST = cssom.parse(_styleText);
                let _generateData = {}
                if (_styleAST && _styleAST.cssRules) {
                    _styleAST.cssRules.forEach(x => {
                        if (/^\./.test(x.selectorText) && !/\s/.test( x.selectorText)) {
                            let _tempData = {}
                            for (let key in x.style) {
                                if (_reg.test(key) && !/^-/.test(key)) {
                                    switch (key) {
                                    case 'margin':
                                        let _tempArrMargin = x.style[key].split(' ')
                                        if (_tempArrMargin.length === 1) {
                                            _tempData['marginTop'] = _tempArrMargin[0];
                                            _tempData['marginLeft'] = _tempArrMargin[0];
                                            _tempData['marginRight'] = _tempArrMargin[0];
                                            _tempData['marginBottom'] = _tempArrMargin[0];
                                        } else if (_tempArrMargin.length === 2){
                                            _tempData['marginTop'] = _tempArrMargin[0];
                                            _tempData['marginLeft'] = _tempArrMargin[1];
                                            _tempData['marginRight'] = _tempArrMargin[1];
                                            _tempData['marginBottom'] = _tempArrMargin[0];
                                        } else if (_tempArrMargin.length === 4) {
                                            _tempData['marginTop'] = _tempArrMargin[0];
                                            _tempData['marginRight'] = _tempArrMargin[1];
                                            _tempData['marginBottom'] = _tempArrMargin[2];
                                            _tempData['marginLeft'] = _tempArrMargin[3];
                                        }
                                        break;
                                    case 'padding':
                                        let _tempArrPadding = x.style[key].split(' ')
                                        if (_tempArrPadding.length === 1) {
                                            _tempData['paddingTop'] = _tempArrPadding[0];
                                            _tempData['paddingLeft'] = _tempArrPadding[0];
                                            _tempData['paddingRight'] = _tempArrPadding[0];
                                            _tempData['paddingBottom'] = _tempArrPadding[0];
                                        }  else if (_tempArrPadding.length === 2){
                                            _tempData['paddingTop'] = _tempArrPadding[0];
                                            _tempData['paddingLeft'] = _tempArrPadding[1];
                                            _tempData['paddingRight'] = _tempArrPadding[1];
                                            _tempData['paddingBottom'] = _tempArrPadding[0];
                                        } else if (_tempArrPadding.length === 4) {
                                            _tempData['paddingTop'] = _tempArrPadding[0];
                                            _tempData['paddingRight'] = _tempArrPadding[1];
                                            _tempData['paddingBottom'] = _tempArrPadding[2];
                                            _tempData['paddingLeft'] = _tempArrPadding[3];
                                        }
                                        break;
                                    case 'border':
                                        let _tempArrBorder = x.style[key].split(' ')
                                        if (_tempArrBorder.length === 3) {
                                            _tempData['borderWidth'] = _tempArrBorder[0];
                                            _tempData['borderStyle'] = _tempArrBorder[1];
                                            _tempData['borderColor'] = _tempArrBorder[2];
                                        }
                                        break;
                                    case 'font-family':
                                        _tempData['fontFamily'] = x.style[key].replace(/"|'/g, "");
                                        break;
                                    default:
                                        _tempData[util.toHump(key)] = x.style[key]
                                    }
                                }
                            }
                            _generateData[x.selectorText.replace(/./, '').replace(/(\[.+\])/, '')] = _tempData
                        }
                    })

                    let _temp = global._bili_cache_data[global._bili_cache_data.coverMapping[that.resourcePath].shortId];
                    if (!_temp) {
                        Object.assign(_generateData, commonStyle);
                        global._bili_cache_data[global._bili_cache_data.coverMapping[that.resourcePath].shortId] = _generateData
                    } else {
                        Object.assign(_temp, _generateData);
                    }
                    // console.log('cover-view抽离样式结果：')
                    // console.log(_generateData)
                }
            }
        });
    }
return source;
}
