<commit_before>import {Component, Input, EventEmitter, Output, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {MdDialog, MdDialogRef} from '@angular/material';
import {DialogType} from '../../../shared/constants';
import {AppState} from '../../../app.service';
import {EmailDialogComponent} from '../../emailDialog';
import {EmailFolderDialogComponent} from '../../emailFolderDialog';
import {EmailService} from '../../shared';

@Component({
  selector: 'navbar',  // <navbar></navbar>
  providers: [],
  styleUrls: ['./navbar.component.css'],
  templateUrl: './navbar.component.html'
})
export class NavBarComponent {
  private navbarItems: any[] = [];
  @Input() lastSync: Date;
  @Input() syncing: Boolean;
  @Output() onRefresh = new EventEmitter<boolean>();

  boxName: string;
  boxList: any[];
  user: any;

  constructor(public appState: AppState, public router: Router, public dialog: MdDialog) {
  }

  ngOnInit() {
    this.appState.dataChange.subscribe((stateChange) => {
      if (this.appState.getBoxList().length > 0) {
        this.boxList = this.appState.getBoxList();
        this.addDataToBoxes(this.appState.getBoxList());
      }
      this.user = this.appState.getUser();
    })
  }

  addDataToBoxes(boxList: any[]) {
    if (boxList.length > 0) {
      boxList = boxList.map((box) => {
        let icon;
        switch (box.shortName) { //TODO put in service
          case 'INBOX':
            icon = 'home';
            break;
          case 'Sent Mail':
            icon = 'send';
            break;
          case 'Drafts':
            icon = 'drafts';
            break;
          case 'Starred':
            icon = 'star';
            break;
          case 'Spam':
            icon = 'error';
            break;
          case 'Trash':
            icon = 'delete';
            break;
          default:
            icon = 'home';
            break;
        };
        box.route = `/box/${box._id}`;
        box.icon = icon;
        box.children = [];
        return box;
      });

      const boxParentMap = new Map();
      boxList.forEach(box => {
        if(boxParentMap.has(box.parent)) 
          boxParentMap.get(box.parent).push(box);
        else
          boxParentMap.set(box.parent, [box]);
      });
      const rootBoxes = boxParentMap.get(null);   

      this.navbarItems = this._populateBoxesTree(rootBoxes, boxParentMap);
      console.log(JSON.stringify(this.navbarItems));
    }
  }

  _populateBoxesTree(boxes: any[], boxParentMap) {
    if(boxes == null)
      return [];
    return boxes.map(box => {
      if(boxParentMap.has(box._id)) {
        let children = boxParentMap.get(box._id);
        box.children.push(this._populateBoxesTree(children, boxParentMap));
      }
      return box;
    });
  }

  refresh() {
    this.onRefresh.emit(true);
  }

  openCreateEmailDialog() {
    let emailDialogRef: MdDialogRef<EmailDialogComponent> = this.dialog.open(EmailDialogComponent, {
      width: '80%',
      height: '95%',
      position: {
        top: '',
        bottom: '',
        left: '',
        right: ''
      }
    });
  }

  openEmailFolderDialog() {
    let dialogRef = this.dialog.open(EmailFolderDialogComponent, {
      width: '50%',<commit_msg>box refactoring fix<commit_after>import {Component, Input, EventEmitter, Output, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {MdDialog, MdDialogRef} from '@angular/material';
import {DialogType} from '../../../shared/constants';
import {AppState} from '../../../app.service';
import {EmailDialogComponent} from '../../emailDialog';
import {EmailFolderDialogComponent} from '../../emailFolderDialog';
import {EmailService} from '../../shared';

@Component({
  selector: 'navbar',  // <navbar></navbar>
  providers: [],
  styleUrls: ['./navbar.component.css'],
  templateUrl: './navbar.component.html'
})
export class NavBarComponent {
  private navbarItems: any[] = [];
  @Input() lastSync: Date;
  @Input() syncing: Boolean;
  @Output() onRefresh = new EventEmitter<boolean>();

  boxName: string;
  boxList: any[];
  user: any;

  constructor(public appState: AppState, public router: Router, public dialog: MdDialog) {
  }

  ngOnInit() {
    this.appState.dataChange.subscribe((stateChange) => {
      if (this.appState.getBoxList().length > 0) {
        this.boxList = this.appState.getBoxList();
        this.addDataToBoxes(this.appState.getBoxList());
      }
      this.user = this.appState.getUser();
    })
  }

  addDataToBoxes(boxList: any[]) {
    if (boxList.length > 0) {
      boxList = boxList.map((box) => {
        let icon;
        switch (box.shortName) { //TODO put in service
          case 'INBOX':
            icon = 'home';
            break;
          case 'Sent Mail':
            icon = 'send';
            break;
          case 'Drafts':
            icon = 'drafts';
            break;
          case 'Starred':
            icon = 'star';
            break;
          case 'Spam':
            icon = 'error';
            break;
          case 'Trash':
            icon = 'delete';
            break;
          default:
            icon = 'home';
            break;
        };
        box.route = `/box/${box._id}`;
        box.icon = icon;
        box.children = [];
        return box;
      });

      const boxParentMap = new Map();
      boxList.forEach(box => {
        if(boxParentMap.has(box.parent)) 
          boxParentMap.get(box.parent).push(box);
        else
          boxParentMap.set(box.parent, [box]);
      });
      const rootBoxes = boxParentMap.get(null);   

      this.navbarItems = this._populateBoxesTree(rootBoxes, boxParentMap);
      console.log(JSON.stringify(this.navbarItems));
    }
  }

  _populateBoxesTree(boxes: any[], boxParentMap) {
    if(boxes == null)
      return [];
    return boxes.map(box => {
      if(boxParentMap.has(box._id)) {
        let children = boxParentMap.get(box._id);
        box.children = this._populateBoxesTree(children, boxParentMap);
      }
      return box;
    });
  }

  refresh() {
    this.onRefresh.emit(true);
  }

  openCreateEmailDialog() {
    let emailDialogRef: MdDialogRef<EmailDialogComponent> = this.dialog.open(EmailDialogComponent, {
      width: '80%',
      height: '95%',
      position: {
        top: '',
        bottom: '',
        left: '',
        right: ''
      }
    });
  }

  openEmailFolderDialog() {
    let dialogRef = this.dialog.open(EmailFolderDialogComponent, {
      width: '50%',
{'fraction_non_alphanumeric': 0.15204678362573099, 'fraction_numerical': 0.0024622960911049553, 'mean_word_length': 2.246253746253746, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1366114', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1972, 'n_words': 400}