/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */
import React, { Component } from 'react';
import { Dimensions } from 'react-native';
import Constants from 'expo-constants';
import BreezeScreenView from './breezeScreenView';
import localData from '../../../../Database/chatter';
interface AppState {
  cameraToggle: boolean;
  shareToggle: boolean;
  searchToggle: boolean;
  chatTextUpdate: string;
  chatMessage: Array<any>;
}

// tslint:disable-next-line:max-line-length
const keyboardOffSetAndroid = Constants.platform
  ? Constants.platform.android
    ? Dimensions.get('screen').height -
    Dimensions.get('window').height -
    Constants.statusBarHeight
    : 0
  : null;

export default class BreezeScreenContainer extends Component<any, AppState> {
  constructor(props: Readonly<{}>) {
    super(props);
    this.state = {
      cameraToggle: false,
      shareToggle: false,
      searchToggle: false,
      chatTextUpdate: '',
      chatMessage: [
        {
          userId: this.leftUser,
          userName: this.leftUser,
          userAvatar: require('../../../../assets/chats/boy.png'),
          time: '10:26 AM',
          message: 'hii !!',
        },
        {
          userId: this.leftUser,
          userName: this.leftUser,
          userAvatar: require('../../../../assets/chats/boy.png'),
          message: 'you in office ?',
        },
        {
          userId: this.leftUser,
          userName: this.leftUser,
          userAvatar: require('../../../../assets/chats/boy.png'),
          message: 'plz share the images ?',
        },
        {
          userId: this.rightUser,
          userName: this.rightUser,
          userAvatar: require('../../../../assets/chats/boy.png'),
          time: '10:40 AM',
          message: 'Sure !',
        },
        {
          userId: this.rightUser,
          userName: this.rightUser,
          userAvatar: require('../../../../assets/chats/boy.png'),
          message: 'hjghj',
        },
      ],
    };
  }

  leftUser = 'Aditya';
  rightUser = 'Enappd';
  userDetails = {
    firstName: 'Bill',
    lastName: 'Kenny',
    image: require('../../../../assets/chats/boy.png'),
    status: 'Online',
  };
  cameraGallery = localData.breezeCameragallery;
  shareAvatar = localData.breezeShareAvatar;

  toogleFabIcon = (item: string) => {
    const { cameraToggle, shareToggle, searchToggle } = this.state;
    switch (item) {
      case 'cameraToggle':
        return this.setState({ cameraToggle: !cameraToggle });

      case 'shareToggle':
        return this.setState({ shareToggle: !shareToggle });

      case 'searchToggle':
        return this.setState({ searchToggle: !searchToggle });

      default:
        return '';
    }
  };

  textChange(event: any) {
    this.setState({ chatTextUpdate: event });
  }

  addChat = () => {
    if (this.state.chatTextUpdate !== '' && this.state.chatTextUpdate.trim()) {
      let localTime = '';
      const senderMessageId = 'Enappd';
      const userCheck = this.state.chatMessage[
        this.state.chatMessage.length - 1
      ].userId;

      if (userCheck === senderMessageId) {
        localTime = '';
      } else {
        localTime = '10: 26 AM';
      }
      this.setState((prevState) => ({
        chatMessage: [
          ...prevState.chatMessage,
          {
            userId: this.rightUser,
            userName: this.rightUser,
            userAvatar: require('../../../../assets/chats/boy.png'),
            time: localTime,
            message: this.state.chatTextUpdate,
          },
        ],
      }));
      this.setState({ chatTextUpdate: '' });
      setTimeout(
        () =>
          this.setState((prevState) => ({
            chatMessage: [
              ...prevState.chatMessage,
              {
                userId: this.leftUser,
                userName: this.leftUser,
                userAvatar: require('../../../../assets/chats/boy.png'),
                time: '10:26 AM',
                message:
                  "Sorry, I don't know the answer to that. Please ask something else",
              },
            ],
          })),
        300
      );
    }
  };

  openSideMenu = () => {
    this.props.navigation.toggleDrawer();
  };

  render() {
    return (
      <BreezeScreenView
        userDetails={this.userDetails}
        cameraToggle={this.state.cameraToggle}
        shareToggle={this.state.shareToggle}
        searchToggle={this.state.searchToggle}
        cameraGallery={this.cameraGallery}
        shareAvatar={this.shareAvatar}
        chatMessage={this.state.chatMessage}
        toogleFabIcon={(item: string) => this.toogleFabIcon(item)}
        addChat={this.addChat}
        chatInput={this.state.chatTextUpdate}
        textChange={($event: any) => this.textChange($event)}
        openSideMenu={this.openSideMenu}
        keyboardOffset={keyboardOffSetAndroid}
      />
    );
  }
}
