# a "view" module generated by Pyrustic Manager
import tkinter as tk
from pyrustic.view import View
from pyrustic.threadom import Threadom
from hubstore.view.init_geet_view import InitGeetView
from hubstore.view.header_view import HeaderView
from hubstore.view.central_view import CentralView
from hubstore.view.footer_view import FooterView
from hubstore.host.main_host import MainHost


class MainView(View):

    def __init__(self, app):
        super().__init__()
        self._app = app
        self._root = app.root
        self._body = None
        self._threadom = Threadom(self._root)
        self._main_host = MainHost()
        self._init_geet_view = None
        self._header_view = None
        self._central_view = None
        self._footer_view = None

    # === PROPERTIES ===

    @property
    def threadom(self):
        return self._threadom

    @property
    def main_host(self):
        return self._main_host

    @property
    def header_view(self):
        return self._header_view

    @property
    def central_view(self):
        return self._central_view

    @property
    def footer_view(self):
        return self._footer_view

    # === PUBLIC ===
    def leave(self):
        self._app.exit()

    # === LIFECYCLE ===

    def _on_build(self):
        # body
        self._body = tk.Frame(self._root)
        # header_view
        self._header_view = HeaderView(self)
        self._header_view.build_pack(fill=tk.X)
        # central_view
        self._central_view = CentralView(self)
        self._central_view.build_pack(fill=tk.BOTH,
                                      expand=1,
                                      pady=(10, 5))
        # footer_view
        self._footer_view = FooterView(self)
        self._footer_view.build_pack(fill=tk.X, anchor="s")

    def _on_display(self):
        self._init_geet()

    def _on_destroy(self):
        pass

    def _init_geet(self):
        if self._main_host.should_init_hubstore():
            init_geet_view = InitGeetView(self)
            init_geet_view.build()
            return
        self._central_view.load_data()
