from .base import *

import calendar

class Rect(object):
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height

    @property
    def ratio(self):
        return (self.width / float(self.height))

    @property
    def ratio_reciprocal(self):
        return 1 / self.ratio

    def __str__(self):
        return "(%s, %s)" % (self.width, self.height)

    @property
    def size(self):
        return (self.width, self.height)

CUT_OFF_DATE_FONT_COLOR = "#A1A8AB"
FULL_NAME_FONT_SIZE = DEFAULT_FONT_SIZE + 7
AMOUNT_FONT_COLOR = "#009888"
FULL_NAME_FONT_COLOR = "#000000"

class ServiceOutlineDocument(PDFDocument):
    def __init__(self, contract):
        self.prepared = False
        self.contract = contract

        super(ServiceOutlineDocument, self).__init__()

    def prepare_pdf_document(self):
        """
        Each tuple consists of ((x, y), font, font-size, wrap-width)
        If wrap-width is None, do not wrap.
        X is the plane left to right, the more the value, the more to right
        Y is the plane down to top, the more the value, the more to the top
        """
        if self.prepared == False:
            page = PDFPage()

            left_x = 70

            page.add_section(
                name='full-name',
                text=self.contract.house.user.get_full_name() or "",
                coords=(left_x, 682),
                font_name='Gotham-Book',
                font_size=FULL_NAME_FONT_SIZE,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=180)
            page.add_section(
                name='address',
                text=self.contract.house.address or "",
                coords=(left_x, 662),
                font_name='Gotham-Book',
                font_size=FULL_NAME_FONT_SIZE,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=180)
            page.add_section(
                name='state-address',
                text=self.contract.house.get_state_address() or "",
                coords=(left_x, 642),
                font_name='Gotham-Book',
                font_size=FULL_NAME_FONT_SIZE,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=180)
            page.add_section(
                name='phone',
                text=self.contract.house.user.phone_number or "",
                coords=(left_x, 622),
                font_name='Gotham-Book',
                font_size=FULL_NAME_FONT_SIZE,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=180)

            if self.contract.house.blue_print:
                bounds = Rect(417, 387)
                coords_y = 227

                wand_image = WandImage(
                    file=self.contract.house.blue_print,
                    format="png"
                )

                #Possible that the image is of poor quality,
                #reload with a new resolution
                if wand_image.size < bounds.size:
                    wand_image.close()

                    wand_image = WandImage(
                        file=self.contract.house.blue_print,
                        format="png",
                        resolution=300
                    )
                    wand_image.compression_quality = 99

                #Trim unneed Whitespace
                wand_image.trim()

                image_rect = Rect(wand_image.width, wand_image.height)

                #If the Width
                if image_rect.ratio > bounds.ratio:
                    #New height = (old height * new width) / old width
                    #New height = (wand_image.width * bounds.height) / wand_image.height
                    new_image_bounds  = Rect(
                        bounds.width,
                        image_rect.ratio_reciprocal * bounds.width
                    )
                elif image_rect.ratio < bounds.ratio:
                    #New width = (old width * new height) / old height
                    #New width = (wand_image.width * bounds.height) / wand_image.height
                    new_image_bounds  = Rect(
                        image_rect.ratio * bounds.height,
                        bounds.height,
                    )
                else:
                    new_image_bounds = image_rect

                image = Image(
                    io.BytesIO(wand_image.make_blob()),
                    #For image to fit in section
                    height=new_image_bounds.height,
                    width=new_image_bounds.width,
                )

                wand_image.close()

                #Try as much to center the image
                coords_x = (left_x + 20 + (bounds.width - new_image_bounds.width) / 2)

                page.add_section(
                    name='blueprint',
                    coords=(coords_x, coords_y),
                    font_name='Gotham-Book',
                    font_size=FULL_NAME_FONT_SIZE,
                    font_color=FULL_NAME_FONT_COLOR,
                    image=image,
                    height=image.imageHeight,
                    width=image.imageWidth
                )

            #Fist page
            self.add_page(page)

            page2 = PDFPage()
            page2.add_section(
                name='lawn-mowing',
                text="Approx XXXX sq. ft. front and XXXX sq. ft. back",
                coords=(275, 632),
                font_name='Gotham-Book',
                font_size=8,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=108)
            page2.add_section(
                name='line-trimming',
                text="Approx XXXX sq. ft. front and XXXX sq. ft. back",
                coords=(275, 607),
                font_name='Gotham-Book',
                font_size=8,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=108)
            page2.add_section(
                name='cleaning-hardscape',
                text="Approx XXXX sq. ft.",
                coords=(275, 492),
                font_name='Gotham-Book',
                font_size=8,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=108)
            page2.add_section(
                name='cleaning-softscape',
                text="Approx XXXX sq. ft.",
                coords=(275, 460),
                font_name='Gotham-Book',
                font_size=8,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=108)
            page2.add_section(
                name='total-per-month',
                text="$254 + TBD",
                coords=(445, 154),
                font_name='Gotham-Medium',
                font_size=10,
                alignment=TA_RIGHT,
                font_color=FULL_NAME_FONT_COLOR,
                wrap=90)


            self.add_page(page2)

    @property
    def filename(self):
        return "Service-Outline,%s.pdf" % self.contract.house.get_blueprint_file_name()

class ServiceOutlineFactory(PDFDocumentFactory):
    def create_service_outline_document(self, contract):
        #1. Create the invoice document using the contract and cut off date provided
        service_outline_document = ServiceOutlineDocument(contract)
        #2. Prepare the PDF Document
        service_outline_document.prepare_pdf_document()

        #Return the invoice document
        return service_outline_document

    def get_template_name(self):
        return 'service_outline_template.pdf'

    def get_attachment_name(self):
        return 'service_outline.pdf'
