# -*- coding: utf-8 -*-

# Copyright(C) 2010  Nicolas Duhamel
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword

from .pages import LoginPage, CookiePage, AccountList, AccountHistory

__all__ = ['BPbrowser']

class BPbrowser(BaseBrowser):
    DOMAIN = 'voscomptesenligne.labanquepostale.fr'
    PROTOCOL = 'https'
    ENCODING = None # refer to the HTML encoding
    PAGES = { r'.*wsost/OstBrokerWeb/loginform.*':                              LoginPage,
              r'.*voscomptes/canalXHTML/releve/syntheseAssurancesEtComptes.ea': CookiePage,
              r'.*voscomptes/canalXHTML/releve/liste_comptes.jsp':              AccountList,
              r'.*canalXHTML/relevesCCP/.*':                                    AccountHistory,
              r'.*canalXHTML/relevesEpargnes/.*':                               AccountHistory,
              
              
            }
    def __init__(self, *args, **kwargs):
        self.inlogin = False
        BaseBrowser.__init__(self, *args, **kwargs)
    
    def home(self):
        self.location("https://voscomptesenligne.labanquepostale.fr/wsost/OstBrokerWeb/loginform?TAM_OP=login&ERROR_CODE=0x00000000&URL=%2Fvoscomptes%2FcanalXHTML%2Fidentif.ea%3Forigin%3Dparticuliers")
      
    def is_logged(self):
        if self.inlogin:
            return True
        return not self.is_on_page(LoginPage)

    def login(self):
        self.inlogin = True
        if not self.is_on_page(LoginPage):
            self.location('https://voscomptesenligne.labanquepostale.fr/wsost/OstBrokerWeb/loginform?TAM_OP=login&ERROR_CODE=0x00000000&URL=%2Fvoscomptes%2FcanalXHTML%2Fidentif.ea%3Forigin%3Dparticuliers')
            
        self.page.login(self.username, self.password)
        self.inlogin = False
        
    def get_accounts_list(self):
        self.location("https://voscomptesenligne.labanquepostale.fr/voscomptes/canalXHTML/authentification/liste_contrat_atos.ea")
        self.location("https://voscomptesenligne.labanquepostale.fr/voscomptes/canalXHTML/releve/liste_comptes.jsp")
        return self.page.get_accounts_list()
    
    def get_account(self, id):
        
        if not self.is_on_page(AccountList):
            self.location("https://voscomptesenligne.labanquepostale.fr/voscomptes/canalXHTML/authentification/liste_contrat_atos.ea")
            self.location("https://voscomptesenligne.labanquepostale.fr/voscomptes/canalXHTML/releve/liste_comptes.jsp")
        return self.page.get_account(id)
     
     
    def get_history(self, Account):
        self.location(Account.link_id)
        return self.page.get_history()

@@ -710,16 +710,18 @@
 7, USA.%0A
+%0A%0A
 from web
@@ -756,16 +756,17 @@
 eBrowser
+#
 , Browse
@@ -856,16 +856,17 @@
 istory%0A%0A
+%0A
 __all__ 
@@ -882,16 +882,17 @@
 wser'%5D%0A%0A
+%0A
 class BP
@@ -1509,178 +1509,24 @@
 ry,%0A
-              %0A              %0A            %7D%0A    def __init__(self, *args, **kwargs):%0A        self.inlogin = False%0A        BaseBrowser.__init__(self, *args, **kwargs)%0A    
+%0A%0A            %7D%0A
 %0A   
@@ -1744,22 +1744,16 @@
 liers%22)%0A
-      
 %0A    def
@@ -1774,57 +1774,8 @@
 f):%0A
-        if self.inlogin:%0A            return True%0A
     
@@ -1842,36 +1842,8 @@
 f):%0A
-        self.inlogin = True%0A
     
@@ -2085,30 +2085,33 @@
 culiers'
-)%0A            
+, no_login=True)%0A
 %0A       
@@ -2161,45 +2161,8 @@
 rd)%0A
-        self.inlogin = False%0A        
 %0A   
@@ -2484,20 +2484,16 @@
 _list()%0A
-    
 %0A    def
@@ -2516,24 +2516,16 @@
 f, id):%0A
-        
 %0A       
@@ -2863,19 +2863,9 @@
 id)%0A
-     %0A     
+%0A
 %0A   

remove useless 'inlogin' attribute