from scaffold.core.widget import base_widget


class control(base_widget):
    method = 'post'
    action = '/'
    inputs = []
    template = {
        'text':'''
            <div class="row">
                <div class="input-field col s12">
                %s
                </div>
            </div>''',
        'radio': '''
                <p>
                %s
                </p>'''}

    def create(self, action, method='post'):
        self.action = action
        self.method = method
        return self

    @staticmethod
    def set_template(template):
        if template:
            control.template = template

    def append(self, input_type, input_name, label, values="", input_id=None, classes='validate', placeholder='', disabled=''):
        if input_type == 'select' and values:
            if type(values) is not list or tuple:
                form_control = """<select name="%s" id="%s">%s</select>""" % (
                    input_name,
                    input_name,
                    "\n".join(["""<option value="%s">%s</option>""" % (value, value) for value in values]))

                self.apply_template(input_type, form_control)
                return self
            if len(values[0]) == 2:
                form_control = """
                    <select name="%s" id="%s">%s</select>""" % (
                        input_name,
                        input_name,
                        "\n".join(["""<option value="%s">%s</option>""" % (value, item) for value, item in values]))
            else:
                form_control = """
                    <select name="%s" id="%s">%s</select>""" % (
                        input_name,
                        input_name,
                        "\n".join(["""<option value="%s">%s</option>""" % (item, item) for item in values]))
                
            self.apply_template(input_type, form_control)
            return self

        form_control = """
            <input type="%s" name="%s" id="%s" placeholder="%s" %s classes="%s" value="%s" />
            <label for="%s">%s</label>""" % (
                input_type, 
                input_name, 
                input_id if input_id else '', 
                label, 
                'disabled="disabled" ' if disabled else '',
                classes,
                values,
                input_id if input_id else '', 
                label)
        self.apply_template(input_type, form_control)
        return self

    def apply_template(self, input_type, form_control):
        return self.inputs.append(
            control.template.get(input_type, "<p>%s</p>") % form_control)

    def render(self):
        super(control, self).render()
        return """
            <div class="row">
                <form class="col s12" method="%s" action="%s">
                    %s
                </form>
            </div>""" % (
                self.method,
                self.action,
                "\n".join([input_item for input_item in self.inputs])
            )
