{% include 'header.html' %}
{% from 'style.html' import breadcrumb_line_svg, circular_arrows_svg, withdraw_svg, utxo_groups_svg, create_utxo_svg, lock_svg, eye_show_svg %}

<div class="container mx-auto">
<section class="p-5 mt-5">
  <div class="flex flex-wrap items-center -m-2">
    <div class="w-full md:w-1/2 p-2">
      <ul class="flex flex-wrap items-center gap-x-3 mb-2">
        <li><a class="flex font-medium text-xs text-coolGray-500 dark:text-gray-300 hover:text-coolGray-700" href="/"><p>Home</p></a></li>
        <li>{{ breadcrumb_line_svg | safe }}</li>
        <li><a class="flex font-medium text-xs text-coolGray-500 dark:text-gray-300 hover:text-coolGray-700" href="/wallets">Wallets</a></li>
        <li>{{ breadcrumb_line_svg | safe }}</li>
      </ul>
    </div>
  </div>
</section>

<section class="py-3">
  <div class="container px-4 mx-auto">
   <div class="relative py-11 px-16 bg-coolGray-900 dark:bg-blue-500 rounded-md overflow-hidden">
      <img class="absolute z-10 left-4 top-4" src="/static/images/elements/dots-red.svg" alt="dots-red">
      <img class="absolute z-10 right-4 bottom-4" src="/static/images/elements/dots-red.svg" alt="dots-red">
      <img class="absolute h-64 left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2 object-cover" src="/static/images/elements/wave.svg" alt="wave">
    <div class="relative z-20 flex flex-wrap items-center -m-3">
     <div class="w-full md:w-1/2 p-3 h-48">
      <h2 class="mb-6 text-4xl font-bold text-white tracking-tighter">Wallets</h2>
      <div class="flex items-center">
        <h2 class="text-lg font-bold text-white tracking-tighter mr-2">Total Assets:</h2>
        <button id="hide-usd-amount-toggle" class="flex items-center justify-center p-1 focus:ring-0 focus:outline-none">{{ eye_show_svg | safe }}</button>
      </div>
      <div class="flex items-baseline mt-2">
        <div id="total-usd-value" class="text-5xl font-bold text-white"></div>
        <div id="usd-text" class="text-sm text-white ml-1">USD</div>
      </div>
        <div id="total-btc-value" class="text-sm text-white mt-2"></div>
     </div>
      <div class="w-full md:w-1/2 p-3 p-6 container flex flex-wrap items-center justify-end items-center mx-auto">
        <a class="rounded-full mr-5 flex flex-wrap justify-center px-5 py-3 bg-blue-500 hover:bg-blue-600 font-medium text-sm text-white border dark:bg-gray-500 dark:hover:bg-gray-700 border-blue-500 rounded-md shadow-button focus:ring-0 focus:outline-none" id="refresh" href="/changepassword">{{ lock_svg | safe }}<span>Change Password</span></a>
        <a class="rounded-full flex flex-wrap justify-center px-5 py-3 bg-blue-500 hover:bg-blue-600 font-medium text-sm text-white border dark:bg-gray-500 dark:hover:bg-gray-700 border-blue-500 rounded-md shadow-button focus:ring-0 focus:outline-none" id="refresh" href="/wallets">{{ circular_arrows_svg | safe }}<span>Refresh</span></a>
     </div>
    </div>
   </div>
  </div>
 </section>

 {% include 'inc_messages.html' %}

 <section class="py-4">
  <div class="container px-4 mx-auto">
   <div class="flex flex-wrap -m-4">
    {% for w in wallets %}
    {% if w.havedata %}
    {% if w.error %}<p>Error: {{ w.error }}</p>
    {% else %}
    <div class="w-full lg:w-1/3 p-4">
     <div class="bg-gray-50 shadow rounded overflow-hidden dark:bg-gray-500">
      <div class="pt-6 px-6 mb-10 flex justify-between items-center">
       <span class="inline-flex items-center justify-center w-9 h-10 bg-white-50 rounded">
        <img class="h-9" src="/static/images/coins/{{ w.name }}.png" alt="{{ w.name }}">
       </span>
       <a class="py-2 px-3 bg-blue-500 text-xs text-white rounded-full hover:bg-blue-600" href="/wallet/{{ w.ticker }}">Manage Wallet</a>
      </div>
      <div class="px-6 mb-6">
       <h4 class="text-xl font-bold dark:text-white">{{ w.name }}
        <span class="inline-block font-medium text-xs text-gray-500 dark:text-white">({{ w.ticker }})</span>
       </h4>
       <p class="text-xs text-gray-500 dark:text-gray-200">Version: {{ w.version }} {% if w.updating %} <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-700 dark:hover:bg-gray-700">Updating..</span></p>
       {% endif %}
      </div>
      <div class="p-6 bg-coolGray-100 dark:bg-gray-600">
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-medium dark:text-white">Balance:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 coinname-value" data-coinname="{{ w.name }}">{{ w.balance }} {{ w.ticker }}</div>
        </div>
         <div class="flex mb-2 justify-between items-center">
           <h4 class="text-xs font-medium dark:text-white">{{ w.ticker }} USD value:</h4>
           <div class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 usd-value"></div>
        </div>
        {% if w.pending %}
         <div class="flex mb-2 justify-between items-center">
           <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Pending:</h4>
           <span class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 coinname-value" data-coinname="{{ w.name }}">+{{ w.pending }} {{ w.ticker }}</span>
          </div>
         <div class="flex mb-2 justify-between items-center">
           <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Pending USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 usd-value"></div>
         </div>
       {% endif %}
       {% if w.cid == '1' %} {# PART #}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Blind Balance:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 coinname-value" data-coinname="{{ w.name }}">{{ w.blind_balance }} {{ w.ticker }}</span>
       </div>
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-medium dark:text-white">Blind USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 usd-value"></div>
        </div>
       {% if w.blind_unconfirmed %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Blind Unconfirmed:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 coinname-value" data-coinname="{{ w.name }}" >+{{ w.blind_unconfirmed }} {{ w.ticker }}</span>
       </div>
      <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Blind Unconfirmed USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 usd-value"></div>
        </div>
       {% endif %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Anon Balance:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 coinname-value"  data-coinname="{{ w.name }}">{{ w.anon_balance }} {{ w.ticker }}</span>
       </div>
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-medium dark:text-white">Anon USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 usd-value"></div>
        </div>
       {% if w.anon_pending %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Anon Pending:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 coinname-value"  data-coinname="{{ w.name }}">
        +{{ w.anon_pending }} {{ w.ticker }}</span>
       </div>
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-bold text-green-500 dark:text-green-500">Anon Pending USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 usd-value"></div>
        </div>
       {% endif %}
       {% endif %} {# / PART #}
       {% if w.cid == '3' %} {# LTC #}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">MWEB Balance:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 coinname-value" data-coinname="{{ w.name }}">{{ w.mweb_balance }} {{ w.ticker }}</span>
       </div>
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-medium dark:text-white">MWEB USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200 usd-value"></div>
        </div>
        {% if w.mweb_pending %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-bold text-green-500 dark:text-green-500">MWEB Pending:</h4>
        <span class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 coinname-value"  data-coinname="{{ w.name }}">
        +{{ w.mweb_pending }} {{ w.ticker }}</span>
       </div>
        <div class="flex mb-2 justify-between items-center">
          <h4 class="text-xs font-bold text-green-500 dark:text-green-500">MWEB Pending USD value:</h4>
          <div class="bold inline-block py-1 px-2 rounded-full bg-green-100 text-xs text-green-500 dark:bg-gray-500 dark:text-green-500 usd-value"></div>
        </div>
       {% endif %}
       {% endif %}
       {# / LTC #}
       <hr class="border-t border-gray-100 dark:border-gray-500 my-5">
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Blocks:</h4>
        <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200">{{ w.blocks }}{% if w.known_block_count %} / {{ w.known_block_count }}
        {% endif %}
      </span>
       </div>
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Last Updated:</h4>
        <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200">{{ w.lastupdated }}</span>
       </div>
       {% if w.bootstrapping %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Bootstrapping:</h4>
        <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200">{{ w.bootstrapping }}</span>
       </div>
       {% endif %}
       {% if w.encrypted %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Locked:</h4>
        <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200">{{ w.locked }}</span>
       </div>
       {% endif %}
       <div class="flex mb-2 justify-between items-center">
        <h4 class="text-xs font-medium dark:text-white">Expected Seed:</h4>
        <span class="inline-block py-1 px-2 rounded-full bg-blue-100 text-xs text-black-500 dark:bg-gray-500 dark:text-gray-200">{{ w.expected_seed }}</span>
       </div>
       <div class="flex justify-between mb-1 mt-10">
        <span class="text-xs font-medium dark:text-gray-200">Blockchain</span>
        <span class="text-xs font-medium dark:text-gray-200">{{ w.synced }}%</span>
       </div>
       <div class="w-full bg-gray-200 rounded-full h-1">
        <div class="bg-blue-500 h-1 rounded-full" style="width: {{ w.synced }}%"></div>
       </div>
      </div>
     </div>
     {% endif %}
     {% endif %}
    </div>
    {% endfor %}
   </div>
 </section>
</div>

{% include 'footer.html' %}

<script>
const coinNameToSymbol = {
  'Bitcoin': 'BTC',
  'Particl': 'PART',
  'Particl Blind': 'PART',
  'Particl Anon': 'PART',
  'Monero': 'XMR',
  'Litecoin': 'LTC',
  'Firo': 'FIRO',
  'Dash': 'DASH',
  'PIVX': 'PIVX'
};

const getUsdValue = (cryptoValue, coinSymbol) => {
  return fetch(`https://min-api.cryptocompare.com/data/price?fsym=${coinSymbol}&tsyms=USD`)
    .then(response => response.json())
    .then(data => {
      const exchangeRate = data.USD;
      if (!isNaN(exchangeRate)) {
        return {
          usdValue: cryptoValue * exchangeRate,
          btcValue: cryptoValue / exchangeRate
        };
      } else {
        throw new Error(`Invalid exchange rate for ${coinSymbol}`);
      }
    });
};

const updateValues = async () => {
  const coinNameValues = document.querySelectorAll('.coinname-value');

  for (const coinNameValue of coinNameValues) {
    const coinFullName = coinNameValue.getAttribute('data-coinname');
    const cryptoValue = parseFloat(coinNameValue.textContent);
    const coinSymbol = coinNameToSymbol[coinFullName];

    if (coinSymbol) {
      try {
        const { usdValue, btcValue } = await getUsdValue(cryptoValue, coinSymbol);

        const usdValueElement = coinNameValue.nextElementSibling.querySelector('.usd-value');
        if (usdValueElement) {
          usdValueElement.textContent = `$${usdValue.toFixed(2)}`;
        }

        const btcValueElement = coinNameValue.nextElementSibling.querySelector('.btc-value');
        if (btcValueElement) {
          btcValueElement.textContent = `${btcValue.toFixed(8)} BTC`;
        }
      } catch (error) {
        console.error(`Error retrieving exchange rate for ${coinSymbol}`);
      }
    } else {
      console.error(`Coin symbol not found for full name: ${coinFullName}`);
    }
  }

  calculateTotalUsdValue();
  calculateTotalBtcValue();
};

const toggleUsdAmount = (usdCell, isVisible) => {
  const usdText = document.getElementById('usd-text');
  if (usdText) {
    usdText.style.display = isVisible ? 'inline' : 'none';
  }

  if (isVisible) {
    const coinFullName = usdCell.previousElementSibling.getAttribute('data-coinname');
    const cryptoValue = parseFloat(usdCell.previousElementSibling.textContent);
    const coinSymbol = coinNameToSymbol[coinFullName];
    if (coinSymbol) {
      updateValues();
    } else {
      console.error(`Coin symbol not found for full name: ${coinFullName}`);
    }
  } else {
    usdCell.textContent = "******";
    const btcValueElement = usdCell.nextElementSibling;
    if (btcValueElement) {
      btcValueElement.textContent = "****";
    }
  }
};

const toggleUsdIcon = (isVisible) => {
  const usdIcon = document.querySelector("#hide-usd-amount-toggle svg");
  if (usdIcon) {
    if (isVisible) {
      usdIcon.innerHTML = `
        <path d="M23.444,10.239C21.905,8.062,17.708,3,12,3S2.1,8.062,.555,10.24a3.058,3.058,0,0,0,0,3.52h0C2.1,15.938,6.292,21,12,21s9.905-5.062,11.445-7.24A3.058,3.058,0,0,0,23.444,10.239ZM12,17a5,5,0,1,1,5-5A5,5,0,0,1,12,17Z"></path>
      `;
    } else {
      usdIcon.innerHTML = `
        <path d="M23.444,10.239a22.936,22.936,0,0,0-2.492-2.948l-4.021,4.021A5.026,5.026,0,0,1,17,12a5,5,0,0,1-5,5,5.026,5.026,0,0,1-.688-.069L8.055,20.188A10.286,10.286,0,0,0,12,21c5.708,0,9.905-5.062,11.445-7.24A3.058,3.058,0,0,0,23.444,10.239Z"></path>
        <path d="M12,3C6.292,3,2.1,8.062,.555,10.24a3.058,3.058,0,0,0,0,3.52h0a21.272,21.272,0,0,0,4.784,4.9l3.124-3.124a5,5,0,0,1,7.071-7.072L8.464,15.536l10.2-10.2A11.484,11.484,0,0,0,12,3Z"></path>
        <path data-color="color-2" d="M1,24a1,1,0,0,1-.707-1.707l22-22a1,1,0,0,1,1.414,1.414l-22,22A1,1,0,0,1,1,24Z"></path>
      `;
    }
  }
};

const calculateTotalUsdValue = async () => {
  const coinNameValues = document.querySelectorAll('.coinname-value');
  let totalUsdValue = 0;

  for (const coinNameValue of coinNameValues) {
    const coinFullName = coinNameValue.getAttribute('data-coinname');
    const cryptoValue = parseFloat(coinNameValue.textContent);
    const coinSymbol = coinNameToSymbol[coinFullName];

    if (coinSymbol) {
      try {
        const { usdValue } = await getUsdValue(cryptoValue, coinSymbol);
        totalUsdValue += usdValue;

        const usdValueElement = coinNameValue.parentElement.nextElementSibling.querySelector('.usd-value');
        if (usdValueElement) {
          usdValueElement.textContent = `$${usdValue.toFixed(2)}`;
        }
      } catch (error) {
        console.error(`Error retrieving exchange rate for ${coinSymbol}`);
      }
    } else {
      console.error(`Coin symbol not found for full name: ${coinFullName}`);
    }
  }

  const totalUsdValueElement = document.getElementById('total-usd-value');
  if (totalUsdValueElement) {
    totalUsdValueElement.textContent = `$${totalUsdValue.toFixed(2)}`;
  }
};

const calculateTotalBtcValue = async () => {
  const usdValueElements = document.getElementsByClassName('usd-value');
  let totalBtcValue = 0;

  try {
    const btcToUsdRate = await fetch('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD')
      .then(response => response.json())
      .then(data => data.USD);

    for (const usdValueElement of usdValueElements) {
      const usdValue = parseFloat(usdValueElement.textContent.replace('$', ''));
      const btcValue = usdValue / btcToUsdRate;

      if (!isNaN(btcValue)) {
        totalBtcValue += btcValue;
      } else {
        console.error(`Invalid USD value: ${usdValue}`);
      }
    }

    const totalBtcValueElement = document.getElementById('total-btc-value');
    if (totalBtcValueElement) {
      if (localStorage.getItem('usdAmountVisible') === 'false') {
        totalBtcValueElement.textContent = "****";
      } else {
        totalBtcValueElement.textContent = `~ ${totalBtcValue.toFixed(8)} BTC`;
      }
    }
  } catch (error) {
    console.error(`Error retrieving BTC to USD exchange rate: ${error}`);
  }
};

const loadUsdAmountVisibility = async () => {
  let usdAmountVisible = localStorage.getItem('usdAmountVisible') === 'true';
  toggleUsdIcon(usdAmountVisible);

  const usdValueElements = document.getElementsByClassName('usd-value');
  for (const usdValueElement of usdValueElements) {
    toggleUsdAmount(usdValueElement, usdAmountVisible, !usdAmountVisible);
  }

  const totalUsdValueElement = document.getElementById('total-usd-value');
  if (!usdAmountVisible && totalUsdValueElement) {
    totalUsdValueElement.textContent = "*****";
  } else if (usdAmountVisible && totalUsdValueElement) {
    await calculateTotalUsdValue();
    calculateTotalBtcValue();
  }
};

window.onload = async () => {
  const hideUsdAmountToggle = document.getElementById('hide-usd-amount-toggle');
  let usdAmountVisible = localStorage.getItem('usdAmountVisible') === 'true';

  hideUsdAmountToggle.addEventListener('click', async () => {
    usdAmountVisible = !usdAmountVisible;
    localStorage.setItem('usdAmountVisible', usdAmountVisible);

    toggleUsdIcon(usdAmountVisible);

    const usdValueElements = document.getElementsByClassName('usd-value');
    for (const usdValueElement of usdValueElements) {
      toggleUsdAmount(usdValueElement, usdAmountVisible);
    }

    const totalUsdValueElement = document.getElementById('total-usd-value');
    if (!usdAmountVisible && totalUsdValueElement) {
      totalUsdValueElement.textContent = "*****";
    } else if (usdAmountVisible && totalUsdValueElement) {
      await calculateTotalUsdValue();
    }

    const totalBtcValueElement = document.getElementById('total-btc-value');
    if (!usdAmountVisible && totalBtcValueElement) {
      totalBtcValueElement.textContent = "";
    } else if (usdAmountVisible && totalBtcValueElement) {
      await calculateTotalBtcValue();
    }
  });

  await loadUsdAmountVisibility();
};
</script>
</body>
</html>
