import React, { useState } from 'react'
import { Avatar, Button, Card, Divider, Dropdown, Input, MenuProps, Radio, RadioChangeEvent, Switch } from 'antd';
import {
  BellOutlined,
  BookOutlined,
  CheckOutlined,
  ClockCircleOutlined,
  CloseOutlined,
  DeleteOutlined,
  MailOutlined,
  MenuOutlined,
  MessageOutlined,
  MoreOutlined,
  PaperClipOutlined,
  PhoneOutlined,
  SearchOutlined,
  SendOutlined,
  StarOutlined,
  UserAddOutlined,
  VideoCameraOutlined
} from '@ant-design/icons';
import '../../styles/pages/chat/Chat.css'

type Contact = {
  id: number;
  name: string;
  avatar: string;
  status: string;
  role: string;
  lastMessage?: string;
  lastMessageTime?: string;
};

type Message = {
  id: number;
  text: string;
  time: string;
  isMe: boolean;
  isRead: boolean;
};

const Chat = () => {

  const [activeContact, setActiveContact] = useState<Contact | null>(null);
  const [userStatus, setUserStatus] = useState('active');
  const [twoStepVerification, setTwoStepVerification] = useState(true);
  const [notificationsEnabled, setNotificationsEnabled] = useState(false);
  const [message, setMessage] = useState('');

  const contacts: Contact[] = [
    { id: 1, name: 'Waldemar Mannering', avatar: '', status: 'online', role: 'AWS Support', lastMessage: 'Refer friends. Get rewards.', lastMessageTime: '5 Minutes' },
    { id: 2, name: 'Felecia Rower', avatar: '', status: 'offline', role: 'Cloud Engineer', lastMessage: 'I will purchase it for sure. 👍', lastMessageTime: '30 Minutes' },
    { id: 3, name: 'Calvin Moore', avatar: '', status: 'busy', role: 'UX Engineer', lastMessage: 'If it takes long you can mail inbox user', lastMessageTime: '1 Day' },
    { id: 4, name: 'Natalie Maxwell', avatar: '', status: 'away', role: 'UI/UX Designer' },
    { id: 5, name: 'Jess Cook', avatar: '', status: 'online', role: 'Business Analyst' },
    { id: 6, name: 'Louie Mason', avatar: '', status: 'offline', role: 'Resource Manager' },
    { id: 7, name: 'Krystal Norton', avatar: '', status: 'online', role: 'Business Executive' },
  ];

  const messages: Message[] = [
    { id: 1, text: 'How can we help? We\'re here for you! 😄', time: '10:00 AM', isMe: true, isRead: true },
    { id: 2, text: 'Hey John, I am looking for the best admin template.', time: '10:02 AM', isMe: false, isRead: true },
    { id: 3, text: 'Could you please help me to find it out? 🤔', time: '10:02 AM', isMe: false, isRead: true },
    { id: 4, text: 'It should be Bootstrap 5 compatible.', time: '10:02 AM', isMe: false, isRead: true },
    { id: 5, text: 'Vuexy has all the components you\'ll ever need in a app.', time: '10:03 AM', isMe: true, isRead: true },
    { id: 6, text: 'Looks clean and fresh UI. 😃', time: '10:05 AM', isMe: false, isRead: true },
    { id: 7, text: 'It\'s perfect for my next project.', time: '10:05 AM', isMe: false, isRead: true },
    { id: 8, text: 'How can I purchase it?', time: '10:05 AM', isMe: false, isRead: true },
    { id: 9, text: 'Thanks, you can purchase it.', time: '10:06 AM', isMe: true, isRead: true },
    { id: 10, text: 'I will purchase it for sure. 👍', time: '10:08 AM', isMe: false, isRead: true },
    { id: 11, text: 'Thanks.', time: '10:08 AM', isMe: false, isRead: true },
    { id: 12, text: 'Great, Feel free to get in touch.', time: '10:10 AM', isMe: true, isRead: true },
    { id: 13, text: 'Do you have design files for Vuexy?', time: '10:15 AM', isMe: false, isRead: true },
    { id: 14, text: 'Yes that\'s correct documentation file, Design files are included with the template.', time: '10:15 AM', isMe: true, isRead: false },
  ];

  const handleStatusChange = (e: RadioChangeEvent) => {
    setUserStatus(e.target.value);
  };

  const handleSendMessage = () => {
    if (message.trim()) {
      // In a real app, you would send the message to your backend here
      setMessage('');
    }
  };

  const contactActions: MenuProps['items'] = [
    { key: '1', label: 'View Contact' },
    { key: '2', label: 'Mute Notifications' },
    { key: '3', label: 'Block Contact' },
    { key: '4', label: 'Clear Chat' },
    { key: '5', label: 'Report' },
  ];

  const rightSidebarActions: MenuProps['items'] = [
    { key: '1', label: 'Add Tag', icon: <BookOutlined /> },
    { key: '2', label: 'Important Contact', icon: <StarOutlined /> },
    { key: '3', label: 'Shared Media', icon: <PaperClipOutlined /> },
    { key: '4', label: 'Block Contact', icon: <CloseOutlined /> },
  ];

  return (
    <div>
      <div className="flex flex-col min-h-screen">
        <div className="flex-grow p-4">
          <Card className="h-full">
            <div className="flex h-full">
              {/* Left Sidebar - User Profile */}
              <div className="w-80 border-r hidden md:flex flex-col">
                <div className="p-6 text-center">
                  <Avatar size={64} src="" className="mb-4" />
                  <h5 className="text-lg font-semibold mb-1">John Doe</h5>
                  <span className="text-gray-500">Admin</span>
                </div>

                <div className="p-6 border-t">
                  <h6 className="text-xs uppercase text-gray-500 mb-2">About</h6>
                  <Input.TextArea
                    rows={3}
                    defaultValue="Hey there, we're just writing to let you know that you've been subscribed to a repository on GitHub."
                    className="mb-2"
                  />
                </div>

                <div className="p-6 border-t">
                  <h6 className="text-xs uppercase text-gray-500 mb-2">Status</h6>
                  <Radio.Group onChange={handleStatusChange} value={userStatus} className="flex flex-col gap-2">
                    <Radio value="active" className="!text-green-500">Online</Radio>
                    <Radio value="away" className="!text-yellow-500">Away</Radio>
                    <Radio value="busy" className="!text-red-500">Do not Disturb</Radio>
                    <Radio value="offline" className="!text-gray-500">Offline</Radio>
                  </Radio.Group>
                </div>

                <div className="p-6 border-t">
                  <h6 className="text-xs uppercase text-gray-500 mb-2">Settings</h6>
                  <div className="space-y-4">
                    <div className="flex justify-between items-center">
                      <div className="flex items-center">
                        <span className="mr-2">Two-step Verification</span>
                      </div>
                      <Switch checked={twoStepVerification} onChange={setTwoStepVerification} />
                    </div>
                    <div className="flex justify-between items-center">
                      <div className="flex items-center">
                        <span className="mr-2">Notification</span>
                      </div>
                      <Switch checked={notificationsEnabled} onChange={setNotificationsEnabled} />
                    </div>
                    <div className="flex items-center cursor-pointer">
                      <UserAddOutlined className="mr-2" />
                      <span>Invite Friends</span>
                    </div>
                    <div className="flex items-center cursor-pointer text-red-500">
                      <DeleteOutlined className="mr-2" />
                      <span>Delete Account</span>
                    </div>
                  </div>
                </div>

                <div className="p-6 border-t mt-auto">
                  <Button type="primary" block>
                    Logout
                  </Button>
                </div>
              </div>

              {/* Contacts List */}
              <div className="w-80 border-r flex flex-col">
                <div className="p-4 border-b flex items-center">
                  <Avatar src="" className="mr-3" />
                  <Input
                    placeholder="Search..."
                    prefix={<SearchOutlined />}
                    className="flex-grow"
                  />
                </div>

                <div className="overflow-y-auto flex-grow">
                  <div className="p-4">
                    <h5 className="text-primary mb-2">Chats</h5>
                    {contacts.filter(c => c.lastMessage).map(contact => (
                      <div
                        key={contact.id}
                        className={`p-3 rounded-lg cursor-pointer hover:bg-gray-100 ${activeContact?.id === contact.id ? 'bg-blue-50' : ''}`}
                        onClick={() => setActiveContact(contact)}
                      >
                        <div className="flex items-center">
                          <Avatar src={contact.avatar} className="mr-3" />
                          <div className="flex-grow">
                            <div className="flex justify-between items-center">
                              <h6 className="font-normal">{contact.name}</h6>
                              <small className="text-gray-500">{contact.lastMessageTime}</small>
                            </div>
                            <small className="text-gray-500 truncate block">{contact.lastMessage}</small>
                          </div>
                        </div>
                      </div>
                    ))}
                  </div>

                  <Divider className="my-0" />

                  <div className="p-4">
                    <h5 className="text-primary mb-2">Contacts</h5>
                    {contacts.map(contact => (
                      <div
                        key={contact.id}
                        className="p-3 rounded-lg cursor-pointer hover:bg-gray-100"
                        onClick={() => setActiveContact(contact)}
                      >
                        <div className="flex items-center">
                          <Avatar src={contact.avatar} className="mr-3" />
                          <div>
                            <h6 className="font-normal">{contact.name}</h6>
                            <small className="text-gray-500">{contact.role}</small>
                          </div>
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              </div>

              {/* Chat Area */}
              {!activeContact ? (
                <div className="flex-grow flex flex-col items-center justify-center">
                  <div className="bg-blue-100 p-8 rounded-full mb-4">
                    <MessageOutlined className="text-4xl text-blue-500" />
                  </div>
                  <p className="mb-4">Select a contact to start a conversation.</p>
                  <Button
                    type="primary"
                    onClick={() => setActiveContact(contacts[0])}
                  >
                    Select Contact
                  </Button>
                </div>
              ) : (
                <div className="flex-grow flex flex-col">
                  {/* Chat Header */}
                  <div className="p-4 border-b flex justify-between items-center">
                    <div className="flex items-center">
                      <MenuOutlined className="mr-3 md:hidden" />
                      <Avatar src={activeContact.avatar} className="mr-3" />
                      <div>
                        <h6 className="font-normal">{activeContact.name}</h6>
                        <small className="text-gray-500">{activeContact.role}</small>
                      </div>
                    </div>
                    <div className="flex items-center">
                      <Button type="text" icon={<PhoneOutlined />} />
                      <Button type="text" icon={<VideoCameraOutlined />} />
                      <Button type="text" icon={<SearchOutlined />} />
                      <Dropdown menu={{ items: contactActions }} trigger={['click']}>
                        <Button type="text" icon={<MoreOutlined />} />
                      </Dropdown>
                    </div>
                  </div>

                  {/* Messages */}
                  <div className="flex-grow overflow-y-auto p-4">
                    {messages.map(msg => (
                      <div
                        key={msg.id}
                        className={`mb-4 flex ${msg.isMe ? 'justify-end' : 'justify-start'}`}
                      >
                        <div className={`max-w-xs md:max-w-md rounded-lg p-3 ${msg.isMe ? 'bg-blue-500 text-white' : 'bg-gray-100'}`}>
                          <p>{msg.text}</p>
                          <div className={`text-xs mt-1 flex items-center ${msg.isMe ? 'text-blue-100 justify-end' : 'text-gray-500'}`}>
                            {msg.isMe && msg.isRead && <CheckOutlined className="mr-1" />}
                            {msg.time}
                          </div>
                        </div>
                      </div>
                    ))}
                  </div>

                  {/* Message Input */}
                  <div className="p-4 border-t">
                    <div className="flex items-center">
                      <Input
                        placeholder="Type your message here..."
                        value={message}
                        onChange={(e) => setMessage(e.target.value)}
                        onPressEnter={handleSendMessage}
                        className="flex-grow mr-2"
                      />
                      <Button type="text" icon={<PaperClipOutlined />} />
                      <Button
                        type="primary"
                        icon={<SendOutlined />}
                        onClick={handleSendMessage}
                      >
                        <span className="hidden md:inline">Send</span>
                      </Button>
                    </div>
                  </div>
                </div>
              )}

              {/* Right Sidebar - Contact Info */}
              {activeContact && (
                <div className="w-80 border-l hidden lg:flex flex-col">
                  <div className="p-6 text-center">
                    <Avatar size={64} src={activeContact.avatar} className="mb-4" />
                    <h5 className="text-lg font-semibold mb-1">{activeContact.name}</h5>
                    <span className="text-gray-500">{activeContact.role}</span>
                  </div>

                  <div className="p-6 border-t">
                    <h6 className="text-xs uppercase text-gray-500 mb-2">About</h6>
                    <p>It is a long established fact that a reader will be distracted by the readable content.</p>
                  </div>

                  <div className="p-6 border-t">
                    <h6 className="text-xs uppercase text-gray-500 mb-2">Personal Information</h6>
                    <div className="space-y-3">
                      <div className="flex items-center">
                        <MailOutlined className="mr-2" />
                        <span>josephGreen@email.com</span>
                      </div>
                      <div className="flex items-center">
                        <PhoneOutlined className="mr-2" />
                        <span>+1(123) 456 - 7890</span>
                      </div>
                      <div className="flex items-center">
                        <ClockCircleOutlined className="mr-2" />
                        <span>Mon - Fri 10AM - 8PM</span>
                      </div>
                    </div>
                  </div>

                  <div className="p-6 border-t">
                    <h6 className="text-xs uppercase text-gray-500 mb-2">Options</h6>
                    <Dropdown menu={{ items: rightSidebarActions }} placement="bottomLeft">
                      <Button type="text" block className="text-left">
                        More Actions
                      </Button>
                    </Dropdown>
                  </div>

                  <div className="p-6 border-t mt-auto">
                    <Button danger block icon={<DeleteOutlined />}>
                      Delete Contact
                    </Button>
                  </div>
                </div>
              )}
            </div>
          </Card>
        </div>
      </div>
    </div>
  )
}

export default Chat