import React, {
  useEffect,
  useState,
} from 'react';

import {
  Link,
  Sheet,
  Toolbar,
  ToolbarPane,
} from 'konsta/react';
import {
  Dices,
  EyeOff,
  XIcon,
} from 'lucide-react';

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { MobileChip } from '@/components/ui/mobile';
import { SystemWindowDialog } from '@/components/ui/system-window-dialog';
import { Lang } from '@/lang/Lang';
import useAppConfigStore from '@/stores/appConfig';

import DataDescription from './components/DataDescription';
import DataLabel from './components/DataLabel';
import { ItemDataProps } from './types';

export const generatePassword = (length: number) => {
  var password = '';
  var characters =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var charactersLength = characters.length;

  for (var i = 0; i < length; i++) {
    password += characters.charAt(
      Math.floor(Math.random() * charactersLength)
    );
  }

  return password;
};
export const PasswordData = (props: ItemDataProps) => {


  const { isMobile } = useAppConfigStore();

  const [localValue, setLocalValue] = useState(props.getValue());

  useEffect(() => {
    props.setValue(localValue);
  }, [localValue]);








  const [passEditModal, setPassEditModal] = useState<boolean>(false);

  const onChangeInputPassword = (e: any) => {
    let val = e.target.value ?? null;

    setLocalValue(val);
  };


  if (props.onlyShow || !props.structure.editable) {

    return <div>
      <DataLabel {...props} />
      <Button variant="secondary" className='w-full flex items-center justify-between'>
        <EyeOff className='text-gray-300 text-base ' />
      </Button>
      <DataDescription {...props} />
    </div>




  }



  return (
    <React.Fragment>
      <DataLabel {...props} />


      {isMobile ? <>

        {!localValue ?
          <Button

            onClick={() => setPassEditModal(true)}>
            {Lang.key('set_password')}
          </Button>
          :
          <MobileChip

            deleteButton
            onDelete={() => setLocalValue(null)}
          >
            {localValue}
          </MobileChip>}

      </> : <>

        {!localValue ? <Button

          onClick={() => setPassEditModal(true)}>
          {Lang.key('set_password')}
        </Button> : (
          <Badge

            onClick={() => {
              setLocalValue(null);
            }}

            className="cursor-pointer"

          >
            {Lang.key('new_password')}: {localValue}
          </Badge>
        )}

      </>}
      <DataDescription {...props} />

      {isMobile ? <Sheet opened={passEditModal} onBackdropClick={() => setPassEditModal(false)}>

        <Toolbar top className="justify-end ios:pt-4">
          <div className="ios:hidden" />
          <ToolbarPane>
            <Link iconOnly onClick={() => setPassEditModal(false)}>
              <XIcon />
            </Link>
          </ToolbarPane>
        </Toolbar>

        <div className='mx-4 my-10'>
          <Input
            className='w-full'
            value={localValue}
            onChange={onChangeInputPassword}
          />

          <Button

            variant="outline"
            style={{ marginTop: 15, width: '100%' }}
            onClick={() => setLocalValue(generatePassword(16))}
          >
            <Dices /> {Lang.key('generate_random_password')}
          </Button>

          <Button
            style={{ marginTop: 15, width: '100%' }}
            onClick={() => setPassEditModal(false)}
          >
            {Lang.key('close')}
          </Button>
        </div>

      </Sheet> : (
        <SystemWindowDialog
          open={passEditModal}
          onOpenChange={setPassEditModal}
          title={Lang.key('set_password')}
          width={448}
          footer={
            <Button onClick={() => setPassEditModal(false)}>
              {Lang.key('close')}
            </Button>
          }
        >
          <div className="space-y-3">
            <Input
              className='w-full'
              value={localValue}
              onChange={onChangeInputPassword}
            />
            <Button
              variant="outline"
              className="w-full"
              onClick={() => setLocalValue(generatePassword(16))}
            >
              <Dices /> {Lang.key('generate_random_password')}
            </Button>
          </div>
        </SystemWindowDialog>
      )}
    </React.Fragment>
  );
}
