import { FileTextFilled } from '@ant-design/icons';
import { useRef, useState } from 'react';
import { IChatItem } from '../ChatLogsType';

export const ChatVoice: React.FC<IChatItem> = (props) => {
  const [open, setOpen] = useState(false);
  const audioRef = useRef<any>();
  function readBlob(blob: Blob, callback: Function) {
    const reader = new FileReader();
    reader.onload = function (e) {
      const data = new Uint8Array(e.target.result);
      callback(data);
    };
    reader.readAsArrayBuffer(blob);
  }

  function content() {
    try {
      const msg = JSON.parse(props.chat?.content as string);

      return (
        <div style={{ display: 'flex', flexDirection: 'column' }}>
          <div
            style={{
              display: 'flex',
              color: '#000',
              justifyContent: 'flex-end',
              alignItems: 'center',
              cursor: 'pointer',
            }}
            onClick={() => {
              const audios = document.querySelectorAll('audio');
              if (audios) {
                audios.forEach((el) => {
                  el.pause();
                });
              }
              if (!open) {
                fetch(`/api/${msg.path}`)
                  .then((response) => response.blob())
                  .then((res) => {
                    let blob = new Blob([res]);
                    readBlob(blob, function (data: any) {
                      if (window.AMR) {
                        var buffer = window.AMR.toWAV(data);
                        var url = URL.createObjectURL(new Blob([buffer], { type: 'audio/x-wav' }));
                        audioRef.current.src = url;
                        audioRef.current.play().catch((err: any) => {
                          console.log(err);
                        });
                      }
                    });
                  });
                setOpen(true);
              } else {
                audioRef.current.play().catch((err: any) => {
                  console.log(err);
                });
              }
            }}
          >
            <span style={{ lineHeight: 1, marginRight: 4, minWidth: 100, textAlign: 'right' }}>
              {msg.play_length}"
            </span>
            <svg viewBox="0 0 1024 1024" width="18" height="18">
              <path
                d="M913.568627 518.334145C913.568627 483.422393 894.958532 451.162604 864.748584 433.706729 834.538616 416.250852 797.318425 416.250852 767.108477 433.706729 736.89851 451.162604 718.288414 483.422393 718.288414 518.334145 718.288414 553.245895 736.89851 585.505684 767.108477 602.961559 797.318425 620.417436 834.538616 620.417436 864.748584 602.961559 894.958532 585.505684 913.568627 553.245895 913.568627 518.334145L913.568627 518.334145ZM581.566143 215.288946C581.566143 215.288946 593.269057 203.098427 620.669932 203.098427 650.6546 203.098427 674.950626 227.414165 674.950626 257.423171 674.950626 290.452615 648.917554 305.250663 636.758739 309.031723 568.451875 353.599725 523.116685 430.436655 523.116685 518.116824 523.116685 605.970743 568.603889 682.938067 637.149606 727.440872 650.915177 731.852057 674.950626 743.064634 674.950626 778.940765 674.950626 808.949772 650.6546 833.265507 620.669932 833.265507 593.269057 833.265507 581.566143 821.07499 581.566143 821.07499 481.233659 757.536806 414.403283 645.801687 414.403283 518.181916 414.403283 390.562145 481.233659 278.805502 581.566143 215.288946L581.566143 215.288946ZM348.376225 14.135304C348.376225 14.135304 365.941399 7.529394 392.690949 7.529394 428.668185 7.529394 457.82781 36.73442 457.82781 72.719107 457.82781 98.186537 406.282822 128.195544 388.54391 137.474179 297.265513 229.695819 240.704994 356.381112 240.704994 496.452082 240.704994 650.321426 309.055287 787.849957 416.661363 881.418878 440.740242 890.980031 457.82781 914.426591 457.82781 941.914889 457.82781 977.899576 428.668185 1007.104602 392.690949 1007.104602 363.314276 1007.104602 345.336511 998.13011 345.336511 998.13011 202.12236 882.635664 110.431373 705.732666 110.431373 507.316947 110.431373 307.532423 203.446734 129.564556 348.376225 14.135304L348.376225 14.135304Z"
                fill="#000000"
              ></path>
            </svg>
          </div>
          <audio
            controls
            style={{ display: open ? 'block' : 'none', marginTop: 8 }}
            ref={audioRef}
          ></audio>
        </div>
      );
    } catch (_e) {
      return (
        <div>
          <div style={{ wordBreak: 'break-all', paddingRight: 8 }}>{props.chat?.content}</div>
          <FileTextFilled style={{ fontSize: 40, color: '#FA9D3B', flexShrink: 0 }} />
        </div>
      );
    }
  }

  return <>{content()}</>;
};
