package sub

import (
	"github.com/fatih/color"
	"golang.flu0r1ne.net/planr"
        "fmt"
        "strings"
)

var (
  col_pass = color.New(color.FgGreen)
  col_fail = color.New(color.FgRed)
  col_title = color.New(color.FgHiWhite)
  col_label = color.New(color.FgCyan)
);

func tcTitle(tr planr.TestResult) string {
  title := tr.Tc.Cname

  if tr.Tc.Config.Title != "" {
    title = tr.Tc.Config.Title
  }

  return title
}

func tcStatus(tc planr.TestResult) string {
    status := "SILENT"

    if tc.Status == planr.PASSING {
      status = "PASS"
    } else {
      status = "FAIL"
    }

    return status
}

func pprintLabeled(label, value string) {
  col_label.Printf("  %s: ", label)
  fmt.Println(value)
}

const (
  FENCE_WIDTH = 78
)

func pprintFenced(title, value string) {
  wingWidth := FENCE_WIDTH - len(title) - 2
  wing := strings.Repeat("-", wingWidth / 2)
  fence := strings.Repeat("-", FENCE_WIDTH) 

  fmt.Printf("  %s %s %s\n", wing, title, wing)
  
  fmt.Print("  " + strings.ReplaceAll(value, "\n", "\n  "))

  fmt.Println(fence)
}

func tcStatusLine(tr planr.TestResult) {
  title  := tcTitle(tr)
  status := tcStatus(tr)

  if status == "PASS" {
    col_pass.Printf("[%s] ", status);
  } else {
    col_fail.Printf("[%s] ", status);
  }

  col_title.Println(title);  
}

type PrintOpts int

const (
  PRINT_CONCISE PrintOpts = 1 << iota
  PRINT_DESCRIPTION
  PRINT_POINTS
)

func (opt PrintOpts) HasFlag(flag PrintOpts) bool {
  return (opt & flag) == flag 
}

func tcPprint(tr planr.TestResult, opt PrintOpts) {
  tcStatusLine(tr)

  tc := tr.Tc

  pprintLabeled("id", tc.Cname)

  if tc.Config.Points != nil && opt.HasFlag(PRINT_POINTS) {
    points := fmt.Sprintf("%.1f", *tc.Config.Points)
    pprintLabeled("points1", points)
  }

  if tc.Config.Description != "" && opt.HasFlag(PRINT_DESCRIPTION) {
    tabbed := strings.ReplaceAll(tc.Config.Description, "\n", "\n  ")

    pprintLabeled("description", tabbed)
  }

  if tr.Status == planr.COMPILATION_FAILURE {
    
    if tr.DebugOutput != "" {
      fmt.Println()
      pprintFenced("compilation output", tr.DebugOutput);
    } else {
      fmt.Println("WARN: No debug output provided")
    }

  } else if tr.Status == planr.RUNTIME_FAILURE {

    if tr.TestOutput != "" {
      fmt.Println()
      pprintFenced("test output", tr.TestOutput);
    }
    
  }

  fmt.Println()
}

func printScoring(score planr.Scoring) {
  col_title.Println("Final Results:")
  
  pprintLabeled("passed", fmt.Sprintf("%d/%d", score.Passed, score.Total));

  percent := score.EarnedPoints / score.TotalPoints * 100

  if score.TotalPoints != 0 {
    pprintLabeled("score", fmt.Sprintf(
      "%.2f/%.2f ~= %.1f%%", score.EarnedPoints, score.TotalPoints, percent,
    ));
  }
}