(ns auto-ap.views.main
  (:require
   [re-frame.core :as re-frame]
   [bidi.bidi :as bidi]
   [auto-ap.routes :as routes]
   [auto-ap.subs :as subs]
   [auto-ap.events :as events]
   [auto-ap.views.utils :refer [active-when= login-url]]
   [auto-ap.entities.companies :as company]
   [auto-ap.views.pages :as pages]))

(defn page->layout [page]
  ({:login :blank
    :check :blank
    :needs-activation :blank
     :index :left-panel
     :invoices :left-panel
     :import-invoices :left-panel
     :unpaid-invoices :left-panel
     :paid-invoices :left-panel
    :admin :admin-left-panel
    :admin-companies :admin-left-panel
    :admin-users :admin-left-panel
    :admin-excel-import :admin-left-panel
    :admin-vendors :admin-left-panel
    :admin-reminders :admin-left-panel
     :new-invoice :blank} page :blank))

(defn login-dropdown []
  (let [user (re-frame/subscribe [::subs/user])
        menu (re-frame/subscribe [::subs/menu])]
    [:div {:id "navMenu", :class "navbar-menu"}
     [:div {:class "navbar-end"}
      (if @user
        [:div {:class (str "navbar-item has-dropdown " (when (get-in @menu [:account :active?]) "is-active"))}
         [:a {:class "navbar-link login" :on-click (fn [e] (re-frame/dispatch [::events/toggle-menu :account]))} (:name @user)]
         [:div {:class "navbar-dropdown"}
          [:a {:class "navbar-item"} "My profile"]
          [:a {:class "navbar-item" :href (bidi/path-for routes/routes :admin)
               :on-click (fn [e] (re-frame/dispatch [::events/toggle-menu :account]))} "Administration"]
          [:hr {:class "navbar-divider"}]
          [:a.navbar-item {:on-click (fn [e] (.preventDefault e) (re-frame/dispatch [::events/logout]))} "Logout"]]]
        [:a.navbar-item {:href login-url} "Login"])]]))

(defmulti layout page->layout)

(defmethod layout :admin-left-panel [ap]
  (let [company (re-frame/subscribe [::subs/company])
        companies (re-frame/subscribe [::subs/companies])
        menu (re-frame/subscribe [::subs/menu])]
    [:div  
     [:nav {:class "navbar has-shadow"}
      [:div {:class "container"}
       [:div {:class "navbar-brand"}
        [:a {:class "navbar-item", :href "../"}
         [:h1 (str "Integreat")]]]
        
       [:div {:class "navbar-burger burger", :data-target "navMenu"}
        [:span]
        [:span]
        [:span]] 
       [login-dropdown]]]
     [:div {:class "columns", :id "mail-app"}
      [:aside {:class "column is-narrow aside menu hero is-fullheight"}
       [:div.main
        [:p.menu-label "General"]
        [:p.menu-item
         [:a {:href (bidi/path-for routes/routes :admin) , :class (str "item" (active-when= ap :admin))}
          [:span {:class "icon"}
           [:i {:class "fa fa-tachometer"}]]
          [:span {:class "name"} "Dashboard"]]]

        [:p.menu-label "Setup"]
        [:ul.menu-list
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :admin-companies) , :class (str "item" (active-when= ap :admin-companies))}
           [:span {:class "icon"}
            [:i {:class "fa fa-star-o"}]]
           
           [:span {:class "name"} "Companies"]]]
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :admin-vendors) , :class (str "item" (active-when= ap :admin-vendors))}
           [:span {:class "icon"}
            [:i {:class "fa fa-star-o"}]]
           
           [:span {:class "name"} "Vendors"]]]
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :admin-users), :class (str "item" (active-when= ap :admin-users))}
           [:span {:class "icon"}
            [:i {:class "fa fa-envelope-o"}]]
           [:span {:class "name"} "Users"]]]
         
         [:ul ]]
        [:p.menu-label "History"]
        [:ul.menu-list
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :admin-reminders) , :class (str "item" (active-when= ap :admin-reminders))}
           [:span {:class "icon"}
            [:i {:class "fa fa-star-o"}]]
           
           [:span {:class "name"} "Reminders"]]]]
        [:p.menu-label "Import"]
        [:ul.menu-list
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :admin-excel-import) , :class (str "item" (active-when= ap :admin-excel-import))}
           [:span {:class "icon"}
            [:i {:class "fa fa-download"}]]
           
           [:span {:class "name"} "Excel Invoices"]]]]]

       [:div.left-nav
        [:div {:class "compose has-text-centered"}
         [:a {:class "button is-danger is-block is-bold" :href (bidi/path-for routes/routes :index)}
          [:span {:class "compose"} "Back"]]]]]
      [:div {:class "column messages hero is-fullheight", :id "message-feed"}
       ^{:key (str "active-page-" (:name @company))}
       [:div.inbox-messages
        [pages/active-page ap]]]]
     [:footer {:class "footer"}
      [:div {:class "container"}
       [:div {:class "content has-text-centered"}
        [:p  
         [:strong "Auto-AP"]"by " 
         [:a {:href "https://github.com/"} "Integreat"]"."]
        [:p  
         [:a {:class "icon", :href "https://github.com/dansup/bulma-templates"}
          [:i {:class "fa fa-github"}]]]]]]
     [:div#dz-hidden]]))

(defmethod layout :left-panel [ap]
  (let [company (re-frame/subscribe [::subs/company])
        companies (re-frame/subscribe [::subs/companies])
        menu (re-frame/subscribe [::subs/menu])]
    [:div  
     [:nav {:class "navbar has-shadow"}
      [:div {:class "container"}
       [:div {:class "navbar-brand"}
        [:a {:class "navbar-item", :href "../"}
         [:h1 (str "Auto-ap")]]]
       [:div {:id "navMenu", :class "navbar-menu "
              :on-click (fn [] (re-frame/dispatch [::events/toggle-menu :company]))}
        [:div.navbar-start
         [:div { :class (str "navbar-item has-dropdown " (when (get-in @menu [:company :active?]) "is-active"))}
          [:a {:class "navbar-link login"} "Company: " (if @company (:name @company)
                                                           "All")]
          [:div {:class "navbar-dropdown"}
           [:a {:class "navbar-item"
                :on-click (fn [] (re-frame/dispatch [::events/swap-company nil]))
                } "All"]
           [:hr {:class "navbar-divider"}]
           (for [{:keys [name id] :as company} @companies]
             ^{:key id }
             [:a {:class "navbar-item"
                  :on-click (fn [] (re-frame/dispatch [::events/swap-company company]))
                  } name])]]]] 
       [:div {:class "navbar-burger burger", :data-target "navMenu"}
        [:span]
        [:span]
        [:span]] 
       [login-dropdown]]]
     [:div {:class "columns", :id "mail-app"}
      [:aside {:class "column is-narrow aside menu hero is-fullheight"}
       [:div.main
        [:p.menu-label "General"]
        [:p.menu-item
         [:a {:href (bidi/path-for routes/routes :index) , :class (str "item" (active-when= ap :index))}
          [:span {:class "icon"}
           [:i {:class "fa fa-tachometer"}]]
          [:span {:class "name"} "Dashboard"]]]

        [:p.menu-label "Accounts Payable"]
        [:ul.menu-list
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :import-invoices) , :class (str "item" (active-when= ap :import-invoices))}
           [:span {:class "icon"}
            [:i {:class "fa fa-star-o"}]]
           
           [:span {:class "name"} "Upload Invoices"]]]
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :unpaid-invoices), :class (str "item" (active-when= ap :unpaid-invoices))}
           [:span {:class "icon"}
            [:i {:class "fa fa-envelope-o"}]]
           [:span {:class "name"} "Unpaid Invoices"]] 
          ]
         [:li.menu-item
          [:a {:href (bidi/path-for routes/routes :paid-invoices), :class (str "item" (active-when= ap :paid-invoices))}
           [:span {:class "icon"}
            [:i {:class "fa fa-envelope-o"}]]
           [:span {:class "name"} "Paid Invoices"]] 
          ]
         [:ul ]]]

       [:div.left-nav
        [:div {:class "compose has-text-centered"}
         [:a {:class "button is-danger is-block is-bold" :href (bidi/path-for routes/routes :new-invoice)}
          [:span {:class "compose"} "New Invoice"]]]]]
      [:div {:class "column messages hero is-fullheight", :id "message-feed"}
       ^{:key (str "active-page-" (:name @company))}
       [:div.inbox-messages
        [pages/active-page ap]]]]
     [:footer {:class "footer"}
      [:div {:class "container"}
       [:div {:class "content has-text-centered"}
        [:p  
         [:strong "Auto-AP"]"by " 
         [:a {:href "https://github.com/"} "Integreat"]"."]
        [:p  
         [:a {:class "icon", :href "https://github.com/dansup/bulma-templates"}
          [:i {:class "fa fa-github"}]]]]]]
     [:div#dz-hidden]]))

(defmethod layout :blank [ap]
  [:div  
   ^{:key ap} [pages/active-page ap]])

(defn page []
  (let [ap (re-frame/subscribe [::subs/active-page])]
    [:div  
     ^{:key @ap} [layout @ap]]))
