# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2016 <Jamie McGowan> <jamiemcgowan.dev@gmail.com>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
### END LICENSE

### DO NOT EDIT THIS FILE ###

from gi.repository import Gtk # pylint: disable=E0611

from . helpers import get_builder

import logging
logger = logging.getLogger('remarkable')

class InsertTableDialog():
    #__gtype_name__ = "InsertTableDialog"
    local_var = -1

    def __init__(self,parent):

        builder = get_builder('InsertTableDialog')
        self.dialog = builder.get_object("insert_table_dialog")
        self.dialog.set_transient_for(parent)
        self.finish_initializing(builder)

    def finish_initializing(self, builder):
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self)
        cancelButton = builder.get_object('btn_cancel')
        cancelButton.connect('clicked', self.on_cancel_clicked)

        self.entry_rows = builder.get_object('entry_rows')
        self.entry_cols = builder.get_object('entry_cols')

        changeButton = builder.get_object('btn_change')
        changeButton.connect('clicked', self.alterGridDisplay)
        self.data_grid = builder.get_object('grid_data')

        self.btn_insert = builder.get_object('btn_insert')
        self.btn_insert.connect('clicked', self.on_insert_clicked)

    def alterGridDisplay(self, widget):
        rows = int(self.entry_rows.get_text())
        cols = int(self.entry_cols.get_text())
        logger.debug('changing grid, rows: %d, cols: %d',rows,cols)

        while True:
            if self.data_grid.get_child_at(0, 0) != None:
                self.data_grid.remove_row(0)
            else:
                break

        for i in range(rows):
            for j in range(cols):
                e = Gtk.Entry()
                self.data_grid.attach(e,j,i,1,1)
        self.data_grid.show_all()

    def run(self):
        return self.dialog.run()

    def on_cancel_clicked(self, widget):
        self.local_var = 42
        self.dialog.response(-1)

    def get_local_var(self):
        return self.local_var

    def get_grid_data(self):
        grid_data = {};
        grid_data['rows']=int(self.entry_rows.get_text())
        grid_data['cols']=int(self.entry_cols.get_text())
        data = []
        for i in range(grid_data['rows']):
            row = []
            for j in range(grid_data['cols']):
                value = self.data_grid.get_child_at(j,i).get_text()
                logger.debug("row: %d, col: %d, value: %s", i,j,value)
                row.append(value)
            data.append(row)

        grid_data['data'] = data
        return grid_data

    def get_grid_tablestring(self):
        rows = int(self.entry_rows.get_text())
        cols = int(self.entry_cols.get_text())
        output = ''
        for i in range(rows):
            for j in range(cols):
                output += ' %s |' % (self.data_grid.get_child_at(j,i).get_text())

            output += '\n'
            if i == 0:
                for k in range(cols):
                    output += '---|'
                output = output[:-1] + '\n'

        return output

    def on_insert_clicked(self, widget):
        self.dialog.response(1)

    def dispose(self):
        self.dialog.close()