import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/npcs/npcsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";


const NpcsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { npcs } = useAppSelector((state) => state.npcs)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

    useEffect(() => {
        dispatch(fetch({ id }));
    }, [dispatch, id]);


    return (
      <>
          <Head>
              <title>{getPageTitle('View npcs')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View npcs')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/npcs/npcs-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Name</p>
                    <p>{npcs?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Age</p>
                  <p>{npcs?.age || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Profession</p>
                    <p>{npcs?.profession ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Personality</p>
                    <p>{npcs?.personality ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>HomeLocation</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{npcs?.home_location?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>WorkLocation</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{npcs?.work_location?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Bio</p>
                  {npcs.bio
                    ? <p dangerouslySetInnerHTML={{__html: npcs.bio}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EmotionState</p>
                    <p>{npcs?.emotion_state ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PortraitImages</p>
                    {npcs?.portrait_images?.length
                      ? (
                        <ImageField
                          name={'portrait_images'}
                          image={npcs?.portrait_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No PortraitImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsMainCharacter'>
                    <SwitchField
                      field={{name: 'is_main_character', value: npcs?.is_main_character}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Family_memberships NPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RoleInFamily</th>
                                
                                
                                
                                <th>IsHeadOfHousehold</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.family_memberships_npc && Array.isArray(npcs.family_memberships_npc) &&
                              npcs.family_memberships_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/family_memberships/family_memberships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="role_in_family">
                                        { item.role_in_family }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_head_of_household">
                                        { dataFormatter.booleanFormatter(item.is_head_of_household) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.family_memberships_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Npc_relationships NPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RelationshipType</th>
                                
                                
                                
                                <th>FriendshipPoints</th>
                                
                                
                                
                                <th>TrustPoints</th>
                                
                                
                                
                                <th>LastInteractionAt</th>
                                
                                
                                
                                <th>IsUnlocked</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.npc_relationships_npc && Array.isArray(npcs.npc_relationships_npc) &&
                              npcs.npc_relationships_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/npc_relationships/npc_relationships-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="relationship_type">
                                        { item.relationship_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="friendship_points">
                                        { item.friendship_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="trust_points">
                                        { item.trust_points }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_interaction_at">
                                        { dataFormatter.dateTimeFormatter(item.last_interaction_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_unlocked">
                                        { dataFormatter.booleanFormatter(item.is_unlocked) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.npc_relationships_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Dialogue_lines NPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>MoodTag</th>
                                
                                
                                
                                
                                
                                <th>Priority</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.dialogue_lines_npc && Array.isArray(npcs.dialogue_lines_npc) &&
                              npcs.dialogue_lines_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/dialogue_lines/dialogue_lines-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="mood_tag">
                                        { item.mood_tag }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="priority">
                                        { item.priority }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.dialogue_lines_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quests GiverNPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>QuestType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>AvailableFrom</th>
                                
                                
                                
                                <th>AvailableUntil</th>
                                
                                
                                
                                <th>RewardMoney</th>
                                
                                
                                
                                <th>RewardReputation</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.quests_giver_npc && Array.isArray(npcs.quests_giver_npc) &&
                              npcs.quests_giver_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quests/quests-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quest_type">
                                        { item.quest_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="available_from">
                                        { dataFormatter.dateTimeFormatter(item.available_from) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="available_until">
                                        { dataFormatter.dateTimeFormatter(item.available_until) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reward_money">
                                        { item.reward_money }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reward_reputation">
                                        { item.reward_reputation }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.quests_giver_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quest_objectives TargetNPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ObjectiveType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TargetQuantity</th>
                                
                                
                                
                                <th>SequenceOrder</th>
                                
                                
                                
                                <th>IsOptional</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.quest_objectives_target_npc && Array.isArray(npcs.quest_objectives_target_npc) &&
                              npcs.quest_objectives_target_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quest_objectives/quest_objectives-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="objective_type">
                                        { item.objective_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_quantity">
                                        { item.target_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sequence_order">
                                        { item.sequence_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_optional">
                                        { dataFormatter.booleanFormatter(item.is_optional) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.quest_objectives_target_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Employees NPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>JobTitle</th>
                                
                                
                                
                                <th>EmploymentStatus</th>
                                
                                
                                
                                <th>DailyWage</th>
                                
                                
                                
                                <th>HiredAt</th>
                                
                                
                                
                                <th>CanOperateRegister</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.employees_npc && Array.isArray(npcs.employees_npc) &&
                              npcs.employees_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/employees/employees-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="job_title">
                                        { item.job_title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="employment_status">
                                        { item.employment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="daily_wage">
                                        { item.daily_wage }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hired_at">
                                        { dataFormatter.dateTimeFormatter(item.hired_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="can_operate_register">
                                        { dataFormatter.booleanFormatter(item.can_operate_register) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.employees_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Sales_transactions CustomerNPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>SoldAt</th>
                                
                                
                                
                                <th>PaymentMethod</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>SubtotalAmount</th>
                                
                                
                                
                                <th>TaxAmount</th>
                                
                                
                                
                                <th>TotalAmount</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.sales_transactions_customer_npc && Array.isArray(npcs.sales_transactions_customer_npc) &&
                              npcs.sales_transactions_customer_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/sales_transactions/sales_transactions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="sold_at">
                                        { dataFormatter.dateTimeFormatter(item.sold_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_method">
                                        { item.payment_method }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal_amount">
                                        { item.subtotal_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_amount">
                                        { item.tax_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_amount">
                                        { item.total_amount }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.sales_transactions_customer_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Festival_attendance NPC</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>AttendanceRole</th>
                                
                                
                                
                                <th>DidAttend</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {npcs.festival_attendance_npc && Array.isArray(npcs.festival_attendance_npc) &&
                              npcs.festival_attendance_npc.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/festival_attendance/festival_attendance-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="attendance_role">
                                        { item.attendance_role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="did_attend">
                                        { dataFormatter.booleanFormatter(item.did_attend) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!npcs?.festival_attendance_npc?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Back'
                    onClick={() => router.push('/npcs/npcs-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

NpcsView.getLayout = function getLayout(page: ReactElement) {
    return (
      <LayoutAuthenticated
        
        permission={'READ_NPCS'}
        
      >
          {page}
      </LayoutAuthenticated>
    )
}

export default NpcsView;