'use strict';
import { Iterables } from '../system';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { Container } from '../container';
import { Explorer, ExplorerNode, ExplorerRefNode, ResourceType } from './explorerNode';
import { CommitFormatter, GitStashCommit, ICommitFormatOptions } from '../gitService';
import { StashFileNode } from './stashFileNode';

export class StashNode extends ExplorerRefNode {

    constructor(
        public readonly commit: GitStashCommit,
        private readonly explorer: Explorer
    ) {
        super(commit.toGitUri());
    }

    get ref(): string {
        return this.commit.sha;
    }

    async getChildren(): Promise<ExplorerNode[]> {
        const statuses = (this.commit as GitStashCommit).fileStatuses;

        // Check for any untracked files -- since git doesn't return them via `git stash list` :(
        const log = await Container.git.getLog(this.commit.repoPath, { maxCount: 1, ref: `${(this.commit as GitStashCommit).stashName}^3` });
        if (log !== undefined) {
            const commit = Iterables.first(log.commits.values());
            if (commit !== undefined && commit.fileStatuses.length !== 0) {
                // Since these files are untracked -- make them look that way
                commit.fileStatuses.forEach(s => s.status = '?');
                statuses.splice(statuses.length, 0, ...commit.fileStatuses);
            }
        }

        const children = statuses.map(s => new StashFileNode(s, this.commit.toFileCommit(s), this.explorer));
        children.sort((a, b) => a.label!.localeCompare(b.label!));
        return children;
    }

    getTreeItem(): TreeItem {
        const item = new TreeItem(CommitFormatter.fromTemplate(this.explorer.config.stashFormat, this.commit, {
            truncateMessageAtNewLine: true,
            dataFormat: Container.config.defaultDateFormat
        } as ICommitFormatOptions), TreeItemCollapsibleState.Collapsed);
        item.contextValue = ResourceType.Stash;
        return item;
    }
}