import sublime
from sublime_plugin import WindowCommand

from ..git_command import GitCommand
from ..constants import MERGE_CONFLICT_PORCELAIN_STATUSES
from ...common import util


class GsMergeCommand(WindowCommand, GitCommand):

    """
    Display a list of branches available to merge against the active branch.
    When selected, perform merge with specified branch.
    """

    def run(self):
        sublime.set_timeout_async(lambda: self.run_async(), 1)

    def run_async(self):
        self._branches = tuple(branch for branch in self.get_branches() if not branch.active)
        self._entries = tuple(self._generate_entry(branch) for branch in self._branches)

        self.window.show_quick_panel(
            self._entries,
            self.on_selection
        )

    @staticmethod
    def _generate_entry(branch):
        entry = branch.name_with_remote
        addl_info = []

        if branch.tracking:
            addl_info.append("tracking remote " + branch.tracking)

        if branch.tracking_status:
            addl_info.append(branch.tracking_status)

        if addl_info:
            entry += " (" + " - ".join(addl_info) + ")"

        return entry

    def on_selection(self, index):
        if index == -1:
            return
        branch = self._branches[index]

        savvy_settings = sublime.load_settings("GitSavvy.sublime-settings")
        try:
            self.git(
                "merge",
                "--log" if savvy_settings.get("merge_log") else None,
                branch.name_with_remote
                )
        finally:
            util.view.refresh_gitsavvy(self.window.active_view(), refresh_sidebar=True)


class GsAbortMergeCommand(WindowCommand, GitCommand):

    """
    Reset all files to pre-merge conditions, and abort the merge.
    """

    def run(self):
        sublime.set_timeout_async(self.run_async, 0)

    def run_async(self):
        self.git("reset", "--merge")
        util.view.refresh_gitsavvy(self.window.active_view(), refresh_sidebar=True)


class GsRestartMergeForFileCommand(WindowCommand, GitCommand):

    """
    Reset a single file to pre-merge condition, but do not abort the merge.
    """

    def run(self):
        sublime.set_timeout_async(self.run_async, 0)

    def run_async(self):
        self._conflicts = tuple(
            f.path for f in self.get_status()
            if (f.index_status, f.working_status) in MERGE_CONFLICT_PORCELAIN_STATUSES
            )

        self.window.show_quick_panel(
            self._conflicts,
            self.on_selection
        )

    def on_selection(self, index):
        if index == -1:
            return
        fpath = self._conflicts[index]
        self.git("checkout", "--conflict=merge", "--", fpath)

        util.view.refresh_gitsavvy(self.window.active_view())

@@ -172,16 +172,70 @@
 rt util%0A
+from ..ui_mixins.quick_panel import show_branch_panel%0A
 %0A%0Aclass 
@@ -549,700 +549,117 @@
    s
-elf.
+how
 _branch
-es = tuple(branch for branch in self.get_branches() if not branch.active)%0A        self._entries = tuple(self._generate_entry(branch) for branch in self._branches)%0A%0A        self.window.show_quick_panel(%0A            self._entries,%0A            self.on_selection%0A        )%0A%0A    @staticmethod%0A    def _generate_entry(branch):%0A        entry = branch.name_with_remote%0A        addl_info = %5B%5D%0A%0A        if branch.tracking:%0A            addl_info.append(%22tracking remote %22 + branch.tracking)%0A%0A        if branch.tracking_status:%0A            addl_info.append(branch.tracking_status)%0A%0A        if addl_info:%0A            entry += %22 (%22 + %22 - %22.join(addl_info) + %22)%22%0A%0A        return entry%0A%0A    def on
+_panel(%0A            self.on_branch_selection,%0A            selected_branch=False)%0A%0A    def on_branch
 _sel
@@ -663,37 +663,38 @@
 selection(self, 
-index
+branch
 ):%0A        if in
@@ -687,35 +687,34 @@
 %0A        if 
-index == -1
+not branch
 :%0A          
@@ -726,48 +726,8 @@
 urn%0A
-        branch = self._branches%5Bindex%5D%0A%0A
     
@@ -954,25 +954,8 @@
 anch
-.name_with_remote
 %0A   

improve GsMergeCommand