import React from 'react';
import { SettingsHeader } from './SessionSettingsHeader';
import { shell } from 'electron';
import { SessionIconButton } from '../icon';
import autoBind from 'auto-bind';
import { SessionNotificationGroupSettings } from './SessionNotificationGroupSettings';
// tslint:disable-next-line: no-submodule-imports
import { BlockedUserSettings } from './BlockedUserSettings';
import { SettingsCategoryPrivacy } from './section/CategoryPrivacy';
import { SettingsCategoryAppearance } from './section/CategoryAppearance';
import { SessionButton, SessionButtonColor, SessionButtonType } from '../basic/SessionButton';
import { getPasswordHash } from '../../data/data';
import { LocalizerKeys } from '../../types/LocalizerKeys';
import { matchesHash } from '../../util/passwordUtils';
export function getMediaPermissionsSettings() {
  return window.getSettingValue('media-permissions');
}
export function getCallMediaPermissionsSettings() {
  return window.getSettingValue('call-media-permissions');
}
export enum SessionSettingCategory {
  Appearance = 'appearance',
  Privacy = 'privacy',
  Notifications = 'notifications',
  MessageRequests = 'messageRequests',
  Blocked = 'blocked',
}
export interface SettingsViewProps {
  category: SessionSettingCategory;
}
interface State {
  hasPassword: boolean | null;
  pwdLockError: string | null;
  mediaSetting: boolean | null;
  callMediaSetting: boolean | null;
  shouldLockSettings: boolean | null;
}
const SessionInfo = () => {
  const openOxenWebsite = () => {
    void shell.openExternal('https://oxen.io/');
  };
  return (
    
      v{window.versionInfo.version}
      
        
      
      {window.versionInfo.commitHash}
    
  );
};
export const PasswordLock = ({
  pwdLockError,
  validatePasswordLock,
}: {
  pwdLockError: string | null;
  validatePasswordLock: () => Promise;
}) => {
  return (
    
      
        {window.i18n('password')}
        
        {pwdLockError && 
{pwdLockError}
}
        
       
     
  );
};
export class SessionSettingsView extends React.Component {
  public settingsViewRef: React.RefObject;
  public constructor(props: any) {
    super(props);
    this.state = {
      hasPassword: null,
      pwdLockError: null,
      mediaSetting: null,
      callMediaSetting: null,
      shouldLockSettings: true,
    };
    this.settingsViewRef = React.createRef();
    autoBind(this);
    void this.hasPassword();
  }
  public componentDidMount() {
    window.addEventListener('keyup', this.onKeyUp);
    const mediaSetting = getMediaPermissionsSettings();
    const callMediaSetting = getCallMediaPermissionsSettings();
    this.setState({ mediaSetting, callMediaSetting });
    setTimeout(() => document.getElementById('password-lock-input')?.focus(), 100);
  }
  public componentWillUnmount() {
    window.removeEventListener('keyup', this.onKeyUp);
  }
  /* tslint:disable-next-line:max-func-body-length */
  public renderSettingInCategory() {
    const { category } = this.props;
    if (this.state.hasPassword === null) {
      return null;
    }
    if (category === SessionSettingCategory.Blocked) {
      // special case for blocked user
      return ;
    }
    if (category === SessionSettingCategory.Appearance) {
      return ;
    }
    if (category === SessionSettingCategory.Notifications) {
      return ;
    }
    if (category === SessionSettingCategory.Privacy) {
      return (
        
      );
    }
    return null;
  }
  public async validatePasswordLock() {
    const enteredPassword = String(
      (document.getElementById('password-lock-input') as HTMLInputElement)?.value
    );
    if (!enteredPassword) {
      this.setState({
        pwdLockError: window.i18n('noGivenPassword'),
      });
      return false;
    }
    // Check if the password matches the hash we have stored
    const hash = await getPasswordHash();
    if (hash && !matchesHash(enteredPassword, hash)) {
      this.setState({
        pwdLockError: window.i18n('invalidPassword'),
      });
      return false;
    }
    // Unlocked settings
    this.setState({
      shouldLockSettings: false,
      pwdLockError: null,
    });
    return true;
  }
  public render() {
    const { category } = this.props;
    const shouldRenderPasswordLock = this.state.shouldLockSettings && this.state.hasPassword;
    const categoryLocalized: LocalizerKeys =
      category === SessionSettingCategory.Appearance
        ? 'appearanceSettingsTitle'
        : category === SessionSettingCategory.Blocked
        ? 'blockedSettingsTitle'
        : category === SessionSettingCategory.Notifications
        ? 'notificationsSettingsTitle'
        : 'privacySettingsTitle';
    return (
      
        
        
          {shouldRenderPasswordLock ? (
            
          ) : (
            
              {this.renderSettingInCategory()}
            
          )}
          
         
    );
  }
  public async hasPassword() {
    const hash = await getPasswordHash();
    this.setState({
      hasPassword: !!hash,
    });
  }
  public onPasswordUpdated(action: string) {
    if (action === 'set' || action === 'change') {
      this.setState({
        hasPassword: true,
        shouldLockSettings: true,
        pwdLockError: null,
      });
    }
    if (action === 'remove') {
      this.setState({
        hasPassword: false,
      });
    }
  }
  private async onKeyUp(event: any) {
    const lockPasswordVisible = Boolean(document.getElementById('password-lock-input'));
    if (event.key === 'Enter' && lockPasswordVisible) {
      await this.validatePasswordLock();
    }
    event.preventDefault();
  }
}