# -*- coding: utf-8 -*-
##HEADER/FOOTER

def get_default_homepage_template():
    template ="""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <link rel="stylesheet" type="text/css"
         href="%(dotdot)s%(STATIC_PATH)sabe.css" />
        <link rel="shortcut icon" href="%(dotdot)s%(STATIC_PATH)sfavicon.ico" />
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <title>SolarCoin Blockchain</title>
    </head>
    <body>
      <!-- navbar -->
      <div class="navbar navbar-inverse navbar-static-top" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="#">SolarCoin Blockchain Explorer</a>
            </div>
            <div class="collapse navbar-collapse">
              <ul class="nav navbar-nav">
                <li><a href="/">Home</a></li>
                <li><a href="/browse_blocks/SolarCoin">Browse Blocks</a></li>
                <li><a href="http://www.solarcoin.org">SolarCoin Homepage</a></li>
              </ul>
            </div><!--/.nav-collapse -->
          </div>
        </div>
    
        <!-- end navbar --> 
    
        <div class="container">
          <div class="row">
          </div>
        %(body)s
        
        <p style="font-size: smaller">
            <span style="font-style: italic">
                We hacked <a href="%(ABE_URL)s">%(APPNAME)s</a>.  A lot.  
            </span>
            
        </p>
    </body>
    </html>
    """
    return template
    

######SEARCH FORM WIDGET
def create_search_form():
    template ="""\
            <div class="row">
              <div class="col-xs-8 col-xs-offset-2\">
                <h2>Search by wallet address, block ID, or transaction ID:</h2>
              </div>
            </div>
            <div class="row search-box">
              <div class="col-xs-8 col-xs-offset-2">
                  <form action="../search">
                    <div class="input-group input-group-lg">
                      <input name ="q" type=\"text\" class=\"form-control\" placeholder=\"Enter at least the first 6 characters\">
                      <span class=\"input-group-btn\">
                       <button class=\"btn btn-default\" type=\"submit\">Go!</button>
                      </span>          
                    </div>
                   </form>
              </div>
            </div>
    """
    return template
    
######SEARCH RESULTS
def show_search_results(error, results):
    print "R %s" % results
    template = create_search_form()
    template +="""\
            <div class=\"row\">
              <div class=\"col-xs-8 col-xs-offset-2\">
    """
    if error is not None:
        template += '<h2 class="error">Please enter search terms.</h2>'
    else:
        template +="""\
            <div class="panel panel-default">
                <div class="panel-heading">Search Results</div>
                    <table class="table">
        """
    for type in results:
        for result in results[type]:
            print "R %s" % result
            template += '<tr><td><a href="%s">%s</a></td></tr>' % (
                result['uri'], result ['name'])
            
    template +="""\
                </table>
    
            </div>
        </div>
        </div>
        </div>
    """
    
    return template
    
#####HOMEPAGE
#FIX ROW/COL
def generate_homepage(total_blocks, total_coins_created, cold_storage_wallets, show_search_form):
    template = ""
    if show_search_form == True:
        template += create_search_form()
    template += generate_summary_blocks_and_coins(total_blocks, total_coins_created, cold_storage_wallets)
    template += generate_cold_storage_table(cold_storage_wallets)
    return template

def generate_cold_storage_table(cold_storage_wallets):
  template = """\
  <div class="panel panel-default">
    <div class="panel-heading cold-storage-heading" style="background-color: #F2B70A"><h4>Cold Storage</h4></div>
      <table class="table">
        <tr><td><strong>Wallet Address</strong></td><td><strong>Balance</strong></td>
  """
  for wallet in cold_storage_wallets:
      if wallet == "Total":
          address_link = wallet
      else:
          address_link = "<a href='../address/%s'>%s" % (wallet, wallet)
      template += "<tr><td>%s</td><td>§%s</td></tr>" % (address_link, cold_storage_wallets[wallet])
  
  template += """\
        </table>
     </div>
  </div>
  """
  return template

def generate_block_pager(basename, hi, count):
    #there's some corner cases that could use fixing, only an issue for coins with <count blocks
    first_link = '<a class="btn btn-lg btn-default" href="%s?hi=%s&count=%s">First</a>' % (basename, str(count), str(count))
    prev_link = '<a class="btn btn-lg btn-default" href="%s?hi=%s&count=%s">Previous</a>' % (basename, str(hi-count), str(count))
    current_link = '<a class="btn btn-lg active btn-default" style="background-color: #F2B70A" href="#">%s-%s</a>' % (str(hi-count), str(hi))
    next_link = '<a class="btn btn-lg btn-default" href="%s?hi=%s&count=%s">Next</a>' % (basename, str(hi+count), str(count))
    latest_link = '<a class="btn btn-lg btn-default" href="%s?count=%s">Most Recent</a>' % (basename, str(count))
    
    pager = """\
    <div class="row block-pager">
      <div class="col-xs-6 col-xs-offset-3">
        <div class="btn-group">
          %s
          %s
          %s
          %s
          %s
        </div> 
      </div>
    </div>""" % (first_link, prev_link, current_link, next_link, latest_link)
    
    
    return pager
    
def generate_browse_blocks_page(block_dict, block_pager):
    template = ""
    template += block_pager
    template +="""\
      <div class="row">
        <div class="col-xs-12">
          <div class="panel panel-default">
            <div class="panel-heading" style="background-color: #F2B70A"><h4>All Bl
ocks</h4></div>
            <table class="
table">
              <tr>
                <td><h4>Block Number</h4></td>
                <td><h4>Creation Time</h4></td>
                <td><h4>Transactions</h4></td>
                <td><h4>Total SLR</h4></td>
              </tr>
    """
    
    for block in sorted(block_dict):
        template += "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"\
          % (block_dict[block]['link'], block_dict[block]['time'], block_dict[block]['num_tx'], block_dict[block]['value'])
    
    template +="""
            </table>
          </div>
        </div>
      </div>
      """
    
    template += block_pager
    return template
    
def generate_block_detail_page(block_details, next_list, tx_ids, txs):
    
    #prev/next
    prev_link = '<a class="btn btn-lg btn-default" href="../block/%s">Previous</a>' % block_details['prev_block_hash']
    current_link = '<a class="btn btn-lg active btn-default" style="background-color: #F2B70A" href="#">Block # %s</a>' % block_details['block_number']
    next_link = ""
    for address in next_list:
        next_link += '<a class="btn btn-lg btn-default" href="%s">Next</a>' % address
    
    template = """\
    <div class="row block-detail-pager">
      <div class="col-xs-4 col-xs-offset-4">
        <div class="btn-group center-block">
          %s
          %s
          %s
        </div> 
      </div>
    </div>""" % (prev_link, current_link, next_link)
    
    #block info
    template +="""\
      <div class="row">
        <div class="col-xs-12">
          <div class="panel panel-default">
            <div class="panel-heading" style="background-color: #F2B70A"><h4>Block Information</h4></div>
            <table class="table">
              <tr><td><h4>Block Number</h4></td><td><h4>%s</h4></td></tr>
              <tr><td><h4>Creation Time</h4></td><td><h4>%s</h4></td></tr>
        