<%- title t('users.edit_sign_in_title') %>

<% if current_user.suggest_secure_login? %>
  <%= t('users.secure_login_explanation_html') %>
<% end %>

<h3><%= t('users.edit_sign_in_password') %></h3>

<% if !current_user.errors.empty? %>
  <%= t('common.error_preamble') %>
  <ul>
    <% current_user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

<% if current_user.encrypted_password.nil? %>
  <p><%= t('users.no_password_set_info') %></p>
<% end %>

<%= form_tag(user_update_password_path, method: :put) do |f| %>
  <% if current_user.encrypted_password.present? %>
    <div><%= label_tag t('activerecord.attributes.user.current_password') %><br>
    <%= password_field_tag :password, nil, autocomplete: 'current-password', required: true %></div>
  <% end %>

  <div><%= label_tag t('activerecord.attributes.user.new_password') %><br>
  <%= password_field_tag :new_password, nil, autocomplete: 'new-password', required: true, minlength: 8 %></div>

  <% # this is provided in an updated devise-i18n-views %>
  <div><%= label_tag t('activerecord.attributes.user.new_password_confirmation', default: 'Password confirmation') %><br>
  <%= password_field_tag :new_password_confirmation, nil, autocomplete: 'new-password', required: true, minlength: 8 %></div>

  <% if current_user.encrypted_password.nil? %>
    <div><%= submit_tag t('users.set_password') %></div>
  <% else %>
    <div><%= submit_tag t('users.update_password') %></div>
  <% end %>
<% end %>

<% if current_user.encrypted_password %>
  <hr>

  <% if current_user.otp_required_for_login %>
    <% if current_user.require_secure_login? %>
      <p>
        <%= t('users.edit_sign_in_2fa_enabled_and_required') %>
      </p>
      <%= form_tag(user_enable_2fa_path, method: :patch, class: 'inline-form') do |f| %>
        <input type="submit" value="<%= t('users.edit_sign_in_regenerate_2fa') %>">
      <% end %>
    <% else %>
      <p>
        <%= t('users.edit_sign_in_2fa_enabled') %>
      </p>
      <%= form_tag(user_disable_2fa_path, method: :patch, class: 'inline-form') do |f| %>
        <input type="submit" value="<%= t('users.edit_sign_in_disable_2fa') %>">
      <% end %>
      <%= form_tag(user_enable_2fa_path, method: :patch, class: 'inline-form') do |f| %>
        <input type="submit" value="<%= t('users.edit_sign_in_regenerate_2fa') %>">
      <% end %>
    <% end %>
  <% else %>
    <%= form_tag(user_enable_2fa_path, method: :patch, class: 'inline-form') do |f| %>
      <p>
        <%= t('users.edit_sign_in_2fa_disabled', site_name:) %>
        <input type="submit" value="<%= t('users.edit_sign_in_enable_2fa') %>">
      </p>
    <% end %>
  <% end %>
<% end %>

<hr>

<% if !current_user.encrypted_password.nil? and !current_user.identities.empty? %>
  <%= form_tag(user_remove_password_path, method: :put, class: 'inline-form') do |f| %>
    <p><%= t('users.remove_password_info') %> <input type="submit" value="<%= t('users.remove_password') %>"></p>
  <% end %>
<% end %>

<h3><%= t('users.edit_sign_in_external') %></h3>

<% if current_user.identities.empty? %>
  <p><%= t('users.external_sign_in_info_no_sign_ins', site_name: site_name) %></p>
<% else %>
  <p><%= t('users.external_sign_in_info_with_sign_ins', site_name: site_name) %></p>
  <ul>
    <% current_user.identities.each do |id| %>
      <li>
        <% if !id.url.nil? %>
          <%= link_to id.pretty_provider, id.url %>
        <% else %>
          <%= id.pretty_provider %>
        <% end %>
        <% if id.syncing %>
          <%= t('users.syncing_indicator') %>
          <%= form_tag(user_update_identity_path, method: :put, class: 'inline-form') do |f| %>
            <input type="hidden" name="provider" value="<%= id.provider %>">
            <input type="hidden" name="syncing" value="0">
            <input type="Submit" value="<%= t('users.disable_syncing') %>">
          <% end %>
        <% else %>
          <%= form_tag(user_update_identity_path, method: :put, class: 'inline-form') do |f| %>
            <input type="hidden" name="provider" value="<%= id.provider %>">
            <input type="hidden" name="syncing" value="1">
            <input type="Submit" value="<%= t('users.enable_syncing') %>">
          <% end %>
        <% end %>
        <% if current_user.identities.size > 1 or !current_user.encrypted_password.nil? %>
          <%= form_tag(user_delete_identity_path, method: :delete, class: 'inline-form') do |f| %>
            <input type="hidden" name="provider" value="<%= id.provider %>">
            <input type="Submit" value="<%= t('users.remove_external_sign_in') %>">
          <% end %>
        <% end %>
      </li>
    <% end %>
  </ul>
<% end %>

<%= render partial: 'add_external_sign_in' %>