/*******************************************************************************
 * Copyright 2017 Bstek
 *
 * 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 com.bstek.ureport.expression.model.expr;

import com.bstek.ureport.build.BindData;
import com.bstek.ureport.build.Context;
import com.bstek.ureport.expression.model.Operator;
import com.bstek.ureport.expression.model.data.BindDataListExpressionData;
import com.bstek.ureport.expression.model.data.ExpressionData;
import com.bstek.ureport.expression.model.data.ObjectExpressionData;
import com.bstek.ureport.expression.model.data.ObjectListExpressionData;
import com.bstek.ureport.model.Cell;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jacky.gao
 * @since 2017年1月15日
 */
public class JoinExpression extends BaseExpression {
    private static final long serialVersionUID = -9045259827109781135L;
    private final List<Operator> operators;
    private final List<BaseExpression> expressions;

    public JoinExpression(List<Operator> operators, List<BaseExpression> expressions) {
        this.operators = operators;
        this.expressions = expressions;
    }

    @Override
    protected ExpressionData<?> compute(Cell cell, Cell currentCell, Context context) {
        if (expressions.size() == 1) {
            return expressions.get(0).compute(cell, currentCell, context);
        }
        List<Object> list = new ArrayList<>();
        for (BaseExpression expression : expressions) {
            ExpressionData<?> data = expression.execute(cell, currentCell, context);
            Object obj = null;
            if (data instanceof ObjectExpressionData) {
                ObjectExpressionData d = (ObjectExpressionData) data;
                obj = d.getData();
            } else if (data instanceof ObjectListExpressionData) {
                ObjectListExpressionData d = (ObjectListExpressionData) data;
                obj = d.getData();
            } else if (data instanceof BindDataListExpressionData) {
                BindDataListExpressionData dataList = (BindDataListExpressionData) data;
                List<BindData> bindList = dataList.getData();
                if (bindList.size() == 1) {
                    BindData bindData = bindList.get(0);
                    obj = bindData.getValue();
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (BindData bd : bindList) {
                        if (sb.length() > 0) {
                            sb.append(",");
                        }
                        sb.append(bd.getValue());
                    }
                    obj = sb.toString();
                }
            }
            if (obj == null) {
                obj = "";
            }
            list.add(obj);
        }
        StringBuilder str = null;
        for (int i = 0; i < list.size(); i++) {
            Object data = list.get(i);
            if (str == null) {
                if (data instanceof String) {
                    str = new StringBuilder("\"" + data + "\"");
                    //str=""+data+"";
                } else {
                    str = new StringBuilder("" + data + "");
                }
            } else {
                Operator op = operators.get(i - 1);
                if (data instanceof String) {
                    str.append(op).append("\"").append(data).append("\"");
                    //str+=""+op+""+data+"";
                } else {
                    str.append(op).append(data);
                }
            }
        }
        assert str != null;
        Object obj = context.evalExpr(str.toString());
        return new ObjectExpressionData(obj);
    }

    public List<BaseExpression> getExpressions() {
        return expressions;
    }
}
