from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, Div, HTML, ButtonHolder, Submit
from django import forms
from django.urls import reverse_lazy
from django.forms.models import inlineformset_factory
from django_addanother.widgets import AddAnotherWidgetWrapper

from orders_app.custom_layout_object import Formset
from orders_app.models import Order, OrderProduct, Customer, Product, Store


class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = ['name', 'address', 'telephone', 'email', 'social_media']

    def __init__(self, *arg, **kwarg):
        super(CustomerForm, self).__init__(*arg, **kwarg)
        self.helper = FormHelper()
        self.fields['name'].label = "Ονοματεπώνυμο"
        self.fields['address'].label = "Διεύθυνση"
        self.fields['telephone'].label = "Τηλέφωνο"
        self.fields['email'].label = "email"
        self.fields['social_media'].label = "Facebook/Instagram κτλ."
        self.helper.layout = Layout(
            Div(
                Field('name'),
                Field('address'),
                Field('telephone'),
                Field('email'),
                Field('social_media'),
                HTML("<br>"),
                ButtonHolder(Submit('submit', 'Αποθήκευση')),
            )
        )


class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name']

    def __init__(self, *arg, **kwarg):
        super(ProductForm, self).__init__(*arg, **kwarg)
        self.helper = FormHelper()
        self.fields['name'].label = "Τύπος Προϊόντος"
        self.helper.layout = Layout(
            Div(
                Field('name'),
                HTML("<br>"),
                ButtonHolder(Submit('submit', 'Αποθήκευση')),
            )
        )


class StoreForm(forms.ModelForm):
    class Meta:
        model = Store
        fields = ['name', 'address', 'telephone']

    def __init__(self, *arg, **kwarg):
        super(StoreForm, self).__init__(*arg, **kwarg)
        self.helper = FormHelper()
        self.fields['name'].label = "Όνομα"
        self.fields['address'].label = "Διεύθυνση"
        self.fields['telephone'].label = "Τηλέφωνο"
        self.helper.layout = Layout(
            Div(
                Field('name'),
                Field('address'),
                Field('telephone'),
                HTML("<br>"),
                ButtonHolder(Submit('submit', 'Αποθήκευση')),
            )
        )


class OrderLine(forms.ModelForm):
    class Meta:
        model = OrderProduct
        fields = ['id', 'order', 'product', 'amount', 'cake_type', 'cake_type_num', 'cake_color', 'cake_color_other', 'fyt_color', 'fyt_color_other', 'cake_flavor', 'cake_img', 'cream_cake_3d', 'cream_cake_height', 'cream_cake_img', 'engagement_cake_type', 'engagement_cake_base', 'engagement_cake_pcs', 'engagement_cake_pcs_other', 'engagement_cake_glaze', 'cup_flavor', 'cup_color', 'cup_color_other', 'cup_type', 'candy_bar_type', 'candy_bar_decor', 'candy_bar_decor_other', 'candy_bar_wrap', 'info']
        widgets = {
            'product': AddAnotherWidgetWrapper(
                forms.Select(attrs={'onchange': 'showFields(this);'}),
                reverse_lazy('orders_app:product_create'),
            ),
            'cake_color': forms.Select(attrs={'onchange': 'showOtherFields(this);'}),
            'fyt_color': forms.Select(attrs={'onchange': 'showOtherFields(this);'}),
            'engagement_cake_pcs': forms.Select(attrs={'onchange': 'showOtherFields(this);'}),
            'cup_color': forms.Select(attrs={'onchange': 'showOtherFields(this);'}),
            'candy_bar_decor': forms.Select(attrs={'onchange': 'showOtherFields(this);'}),
            'info': forms.Textarea(attrs={'rows': 2, 'cols': 30}),
        }

    def __init__(self, *arg, **kwarg):
        super(OrderLine, self).__init__(*arg, **kwarg)
        self.empty_permitted = False
        self.fields['product'].label = "Προϊόν"
        self.fields['amount'].label = "Ποσότητα"
        self.fields['cake_type'].label = "Τύπος"
        self.fields['cake_type_num'].label = "Νούμερο"
        self.fields['cake_color'].label = "Χρώμα"
        self.fields['cake_color_other'].label = "Άλλο Χρώμα"
        self.fields['fyt_color'].label = "Φυτιλάκι-Χρώμα"
        self.fields['fyt_color_other'].label = "Φυτιλάκι-Χρώμα Άλλο"
        self.fields['cake_flavor'].label = "Γεύση"
        self.fields['cake_img'].label = "Φωτογραφία"
        self.fields['cream_cake_3d'].label = "3D"
        self.fields['cream_cake_height'].label = "Ύψος"
        self.fields['cream_cake_img'].label = "Φωτογραφία"
        self.fields['engagement_cake_type'].label = "Τύπος"
        self.fields['engagement_cake_base'].label = "Βάση"
        self.fields['engagement_cake_pcs'].label = "Τεμάχια"
        self.fields['engagement_cake_pcs_other'].label = "Τεμάχια Άλλο"
        self.fields['engagement_cake_glaze'].label = "Γλάσσο/Σαντιγύ"
        self.fields['cup_flavor'].label = "Γεύση"
        self.fields['cup_color'].label = "Χρώμα"
        self.fields['cup_color_other'].label = "Άλλο Χρώμα"
        self.fields['cup_type'].label = "Τύπος"
        self.fields['candy_bar_type'].label = "Τύπος CandyBar"
        self.fields['candy_bar_decor'].label = "Ντεκόρ CandyBar"
        self.fields['candy_bar_decor_other'].label = "Ντεκόρ CandyBar Άλλο"
        self.fields['candy_bar_wrap'].label = "Τύλιγμα CandyBar"
        self.fields['info'].label = "Πληροφορίες"


OrderLineFormSet = inlineformset_factory(
    Order, OrderProduct, form=OrderLine,
    fields=['id', 'order', 'product', 'amount', 'cake_type', 'cake_type_num', 'cake_color', 'cake_color_other', 'fyt_color', 'fyt_color_other', 'cake_flavor', 'cake_img', 'cream_cake_3d', 'cream_cake_height', 'cream_cake_img', 'engagement_cake_type', 'engagement_cake_base', 'engagement_cake_pcs', 'engagement_cake_pcs_other', 'engagement_cake_glaze', 'cup_flavor', 'cup_color', 'cup_color_other', 'cup_type', 'candy_bar_type', 'candy_bar_decor', 'candy_bar_decor_other', 'candy_bar_wrap', 'info'], extra=1, can_delete=True
)

OrderLineFormSetEdit = inlineformset_factory(
    Order, OrderProduct, form=OrderLine,
    fields=['id', 'order', 'product', 'amount', 'cake_type', 'cake_type_num', 'cake_color', 'cake_color_other', 'fyt_color', 'fyt_color_other', 'cake_flavor', 'cake_img', 'cream_cake_3d', 'cream_cake_height', 'cream_cake_img', 'engagement_cake_type', 'engagement_cake_base', 'engagement_cake_pcs', 'engagement_cake_pcs_other', 'engagement_cake_glaze', 'cup_flavor', 'cup_color', 'cup_color_other', 'cup_type', 'candy_bar_type', 'candy_bar_decor', 'candy_bar_decor_other', 'candy_bar_wrap', 'info'], extra=0, can_delete=True
)


class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ['order_status', 'customer_name', 'customer_address', 'customer_telephone', 'customer_email', 'delivery', 'delivery_date', 'store', 'order_info', 'order_products']
        widgets = {
            'delivery_date': forms.DateInput(attrs={'type': 'text', 'id': 'delivery_date', 'class': 'datepicker'}),
            'store': AddAnotherWidgetWrapper(
                forms.Select,
                reverse_lazy('orders_app:store_create'),
            ),
            'order_info': forms.Textarea(attrs={'rows': 2, 'cols': 30}),
            # 'info': forms.Textarea(attrs={'type': 'text', 'id': 'order_date', 'class': 'datepicker'}),
        }

    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = True
        self.helper.render_hidden_fields = True
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-3 create-label'
        self.helper.field_class = 'col-md-9'
        self.helper.attrs['autocomplete'] = 'off'
        self.fields['order_status'].label = "Κατάσταση Παραγγελίας"
        self.fields['customer_name'].label = "Πελάτης"
        self.fields['customer_telephone'].label = "Τηλέφωνο"
        self.fields['customer_address'].label = "Διεύθυνση"
        self.fields['customer_email'].label = "email"
        self.fields['delivery'].label = "Παράδοση"
        self.fields['delivery_date'].label = "Ημέρα Παράδοσης"
        self.fields['store'].label = "Κατάστημα"
        self.fields['order_info'].label = "Σχόλια Παραγγελίας"
        self.helper.layout = Layout(
            Div(
                Field('order_status'),
                Field('customer_name'),
                Field('customer_address'),
                Field('customer_telephone'),
                Field('customer_email'),
                Field('delivery'),
                Field('delivery_date'),
                Field('store'),
                Field('order_info'),
                HTML("<br>"),
                Fieldset('Προϊόντα Παραγγελίας',
                         Formset('order_products')),
                HTML("<br>"),
                ButtonHolder(Submit('submit', 'Αποθήκευση')),
            )
        )
