/*
 * Sonar ESQL Plugin
 * Copyright (C) 2013-2018 Thomas Pohl and EXXETA AG
 * http://www.exxeta.com
 *
 * 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.exxeta.iss.sonar.esql.check;

import java.util.List;

import org.sonar.check.Rule;

import com.exxeta.iss.sonar.esql.api.tree.function.CaseFunctionTree;
import com.exxeta.iss.sonar.esql.api.tree.function.WhenClauseExpressionTree;
import com.exxeta.iss.sonar.esql.api.tree.statement.CaseStatementTree;
import com.exxeta.iss.sonar.esql.api.tree.statement.WhenClauseTree;
import com.exxeta.iss.sonar.esql.api.visitors.DoubleDispatchVisitorCheck;
import com.exxeta.iss.sonar.esql.api.visitors.IssueLocation;
import com.exxeta.iss.sonar.esql.api.visitors.PreciseIssue;
import com.exxeta.iss.sonar.esql.tree.impl.function.WhenClauseExpressionTreeImpl;
import com.exxeta.iss.sonar.esql.tree.impl.statement.WhenClauseTreeImpl;

@Rule(key = "CaseWithoutElse")
public class CaseWithoutElseCheck extends DoubleDispatchVisitorCheck {

	private static final String MESSAGE = "Add the missing \"ELSE\" clause.";

	@Override
	public void visitCaseFunction(CaseFunctionTree tree) {

		if (tree.elseKeyword() == null) {
			List<WhenClauseExpressionTreeImpl> whens = tree.whenClauses();
			if (whens.isEmpty()) {
				addIssue(new PreciseIssue(this, new IssueLocation(tree, MESSAGE)));
			} else {
				WhenClauseExpressionTree lastWhen = whens.get(whens.size() - 1);
				addIssue(new PreciseIssue(this, new IssueLocation(tree.caseKeyword(), lastWhen, MESSAGE)));
			}
		}

		super.visitCaseFunction(tree);
	}

	@Override
	public void visitCaseStatement(CaseStatementTree tree) {
		  if(tree.elseKeyword()==null){
			  List<WhenClauseTreeImpl> whens = tree.whenClauses();
			  if (whens.isEmpty()){
				  addIssue(new PreciseIssue(this, new IssueLocation(tree, MESSAGE)));
			  } else {
				  WhenClauseTree lastWhen = whens.get(whens.size()-1);
				  addIssue(new PreciseIssue(this, new IssueLocation(tree.caseKeyword(), lastWhen, MESSAGE)));
			  }
		  }
		super.visitCaseStatement(tree);
	}

}

@@ -1744,119 +1744,8 @@
 ();%0A
-%09%09%09if (whens.isEmpty()) %7B%0A%09%09%09%09addIssue(new PreciseIssue(this, new IssueLocation(tree, MESSAGE)));%0A%09%09%09%7D else %7B%0A%09
 %09%09%09W
@@ -1808,17 +1808,16 @@
 ) - 1);%0A
-%09
 %09%09%09addIs
@@ -1907,13 +1907,8 @@
 ));%0A
-%09%09%09%7D%0A
 %09%09%7D%0A
@@ -2016,20 +2016,19 @@
 ee) %7B%0A%09%09
-  
 if
+ 
 (tree.el
@@ -2042,22 +2042,23 @@
 rd()
+ 
 ==
+ 
 null)
+ 
 %7B%0A%09%09%09
-  
 List
@@ -2113,126 +2113,8 @@
 %0A%09%09%09
-  if (whens.isEmpty())%7B%0A%09%09%09%09  addIssue(new PreciseIssue(this, new IssueLocation(tree, MESSAGE)));%0A%09%09%09  %7D else %7B%0A%09%09%09%09  
 When
@@ -2161,19 +2161,18 @@
 ze()
--
+ - 
 1);%0A%09%09%09
-%09  
 addI
@@ -2265,17 +2265,8 @@
 ;%0A%09%09
-%09  %7D%0A%09%09  
 %7D%0A%09%09

Remove unreachable code