import npyscreen
import curses
import array

from npyscreen import wgtextbox as textbox

class CGrid(npyscreen.SimpleGrid):
    
    how_exited = True
    additional_y_offset   = 2
    _col_widgets = textbox.Textfield
    
    def __init__(self, screen, col_titles = None,col_widths = None, *args, **keywords):
         
        if col_titles:
            self.col_titles = col_titles
        else:
            self.col_titles = []
            
        if(col_widths):
            self.col_widths = col_widths
        super(CGrid, self).__init__(screen, *args, **keywords)
    # You need to override custom_print_cell to manipulate how
    # a cell is printed. In this example we change the color of the
    # text depending on the string value of cell.
    
    # Hacky solution to color download status
    
    def custom_print_cell(self, actual_cell, cell_display_value):
           actual_cell.color = self.getColor(cell_display_value)

           
    def make_contained_widgets(self):
#        super(CGrid, self).make_contained_widgets()
        
        if self.column_width_requested:
            # don't need a margin for the final column
            self.columns = (self.width + self.col_margin) // (self.column_width_requested + self.col_margin)
        elif self.columns_requested:
            self.columns = self.columns_requested
        else:
            self.columns = self.default_column_number
            
        self._my_widgets = []
        column_width = (self.width + self.col_margin - self.additional_x_offset) // self.columns
        column_width -= self.col_margin
        self._column_width = column_width
        if column_width < 1: raise Exception("Too many columns for space available")
        
        for h in range( (self.height - self.additional_y_offset) // self.row_height ):
            h_coord = h * self.row_height
            row = []
            for cell in range(self.columns):
                x_offset = (sum(self.col_widths[0:cell])+self.col_margin)
                row.append(self._contained_widgets(self.parent, rely=h_coord+self.rely + self.additional_y_offset, relx = self.relx + x_offset + self.additional_x_offset, width=self.col_widths[cell], height=self.row_height))
            self._my_widgets.append(row)
        
        
        if(not self.col_widths or len(self.col_widths) != self.columns):
            self.col_widths = array.array('i',(0 for i in range(0,self.columns)))
            self.col_widths.fill(self._column_width)
            
        self._my_col_titles = []
        for title_cell in range(self.columns):
            x_offset = (sum(self.col_widths[0:title_cell])+self.col_margin)
            self._my_col_titles.append(self._col_widgets(self.parent, rely=self.rely, relx = self.relx + x_offset, width=self.col_widths[title_cell], height=1))
        
    def getColor(self,value):
        switcher = {
        'Online':'GOOD',
        'Offline':'STANDOUT',
        'Pending':'STANDOUT',
        'Requested':'CAUTION',
        'Downloading':'NO_EDIT',
        'Downloaded':'GOOD',
        'Hashing':'NO_EDIT',
        'Succeeded':'GOOD',
        'Corrupted':'DANGER',
        'Failed':'DANGER'
        }
        return switcher.get(value,'DEFAULT')
    
    def update(self, clear=True):
        super(CGrid, self).update(clear = True)
        
        _title_counter = 0
        for title_cell in self._my_col_titles:
            try:
                title_text = self.col_titles[self.begin_col_display_at+_title_counter]
            except IndexError:
                title_text = None
            self.update_title_cell(title_cell, title_text)
            _title_counter += 1
            
        self.parent.curses_pad.hline(self.rely+1, self.relx, curses.ACS_HLINE, self.width)
    
    def update_title_cell(self, cell, cell_title):
        cell.value = cell_title
        cell.update()