/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.kylin.metadata.model;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.apache.kylin.metadata.datatype.DataType;

import static com.google.common.base.Preconditions.*;

/**
 */
@SuppressWarnings("serial")
public class TblColRef implements Serializable {

    private static final String INNER_TABLE_NAME = "_kylin_table";

    // used by projection rewrite, see OLAPProjectRel
    public enum InnerDataTypeEnum {

        LITERAL("_literal_type"), DERIVED("_derived_type");

        private final String dateType;

        private InnerDataTypeEnum(String name) {
            this.dateType = name;
        }

        public String getDataType() {
            return dateType;
        }

        public static boolean contains(String name) {
            return LITERAL.getDataType().equals(name) || DERIVED.getDataType().equals(name);
        }
    }

    // used by projection rewrite, see OLAPProjectRel
    public static TblColRef newInnerColumn(String columnName, InnerDataTypeEnum dataType) {
        ColumnDesc column = new ColumnDesc();
        column.setName(columnName);
        TableDesc table = new TableDesc();
        column.setTable(table);
        TblColRef colRef = new TblColRef(column);
        colRef.markInnerColumn(dataType);
        return colRef;
    }
    
    private static final DataModelDesc UNKNOWN_MODEL = new DataModelDesc();
    
    public static TableRef tableForUnknownModel(String tempTableAlias, TableDesc table) {
        return new TableRef(UNKNOWN_MODEL, tempTableAlias, table);
    }
    
    public static TblColRef columnForUnknownModel(TableRef table, ColumnDesc colDesc) {
        checkArgument(table.getModel() == UNKNOWN_MODEL);
        return new TblColRef(table, colDesc);
    }
    
    public static void fixUnknownModel(DataModelDesc model, String alias, TblColRef col) {
        checkArgument(col.table.getModel() == UNKNOWN_MODEL || col.table.getModel() == model);
        TableRef tableRef = model.findTable(alias);
        checkArgument(tableRef.getTableDesc() == col.column.getTable());
        col.table = tableRef;
    }

    // for test only
    public static TblColRef mockup(TableDesc table, int oneBasedColumnIndex, String name, String datatype) {
        ColumnDesc desc = new ColumnDesc();
        String id = "" + oneBasedColumnIndex;
        desc.setId(id);
        desc.setName(name);
        desc.setDatatype(datatype);
        desc.init(table);
        return new TblColRef(desc);
    }
    
    // ============================================================================

    private TableRef table;
    private ColumnDesc column;

    TblColRef(ColumnDesc column) {
        this.column = column;
    }
    
    TblColRef(TableRef table, ColumnDesc column) {
        checkArgument(table.getTableDesc() == column.getTable());
        this.table = table;
        this.column = column;
    }
    
    public ColumnDesc getColumnDesc() {
        return column;
    }

    public String getName() {
        return column.getName();
    }

    public TableRef getTableRef() {
        return table;
    }
    
    public String getTable() {
        if (column.getTable() == null) {
            return null;
        }
        return column.getTable().getIdentity();
    }

    public String getCanonicalName() {
        return getTable() + "." + getName();
    }

    public String getDatatype() {
        return column.getDatatype();
    }

    public DataType getType() {
        return column.getType();
    }

    public void markInnerColumn(InnerDataTypeEnum dataType) {
        this.column.setDatatype(dataType.getDataType());
        this.column.getTable().setName(INNER_TABLE_NAME);
        this.column.getTable().setDatabase("DEFAULT");
    }

    public boolean isInnerColumn() {
        return InnerDataTypeEnum.contains(getDatatype());
    }

    public boolean isDerivedDataType() {
        return InnerDataTypeEnum.DERIVED.getDataType().equals(getDatatype());
    }

    /**
     *
     * @param tableName full name : db.table
     * @param columnName columnname
     * @return
     */
    public boolean isSameAs(String tableName, String columnName) {
        return column.isSameAs(tableName, columnName);
    }

    @Override
    public int hashCode() {
        // NOTE: tableRef MUST NOT participate in hashCode().
        // Because fixUnknownModel() can change tableRef while TblColRef is held as set/map keys.
        final int prime = 31;
        int result = 1;

        result = prime * result + column.getTable().getIdentity().hashCode();
        result = prime * result + column.getName().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TblColRef other = (TblColRef) obj;
        if (!StringUtils.equals(column.getTable().getIdentity(), other.column.getTable().getIdentity()))
            return false;
        if (!StringUtils.equals(column.getName(), other.column.getName()))
            return false;
        return true;
    }

    @Override
    public String toString() {
        String alias = table == null ? "UNKNOWN-MODEL" : table.getAlias();
        if (alias.equals(column.getTable().getName())) {
            return column.getTable().getIdentity() + "." + column.getName();
        } else {
            return alias + ":" + column.getTable().getIdentity() + "." + column.getName();
        }
    }
}

@@ -843,16 +843,83 @@
 model;%0A%0A
+import static com.google.common.base.Preconditions.checkArgument;%0A%0A
 import j
@@ -1041,63 +1041,8 @@
 e;%0A%0A
-import static com.google.common.base.Preconditions.*;%0A%0A
 /**%0A

fix checkstyle