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/threat_actors/threat_actorsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";


const Threat_actorsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { threat_actors } = useAppSelector((state) => state.threat_actors)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

    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 threat_actors')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View threat_actors')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/threat_actors/threat_actors-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ActorType</p>
                    <p>{threat_actors?.actor_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Motivation</p>
                    <p>{threat_actors?.motivation ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginCountry</p>
                    <p>{threat_actors?.origin_country}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={threat_actors?.aliases} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ThreatLevel</p>
                  <p>{threat_actors?.threat_level || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='FirstSeenAt'>
                    {threat_actors.first_seen_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={threat_actors.first_seen_at ?
                        new Date(
                          dayjs(threat_actors.first_seen_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No FirstSeenAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='LastSeenAt'>
                    {threat_actors.last_seen_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={threat_actors.last_seen_at ?
                        new Date(
                          dayjs(threat_actors.last_seen_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No LastSeenAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Threat_indicators ThreatActor</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>IndicatorValue</th>
                                
                                
                                
                                <th>IndicatorType</th>
                                
                                
                                
                                <th>Confidence</th>
                                
                                
                                
                                <th>Severity</th>
                                
                                
                                
                                
                                
                                <th>FirstSeenAt</th>
                                
                                
                                
                                <th>LastSeenAt</th>
                                
                                
                                
                                <th>Source</th>
                                
                                
                                
                                <th>Context</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {threat_actors.threat_indicators_threat_actor && Array.isArray(threat_actors.threat_indicators_threat_actor) &&
                              threat_actors.threat_indicators_threat_actor.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/threat_indicators/threat_indicators-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="indicator_value">
                                        { item.indicator_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="indicator_type">
                                        { item.indicator_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidence">
                                        { item.confidence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="severity">
                                        { item.severity }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="first_seen_at">
                                        { dataFormatter.dateTimeFormatter(item.first_seen_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_seen_at">
                                        { dataFormatter.dateTimeFormatter(item.last_seen_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="source">
                                        { item.source }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="context">
                                        { item.context }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="active">
                                        { dataFormatter.booleanFormatter(item.active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!threat_actors?.threat_indicators_threat_actor?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Threat_campaigns ThreatActor</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>CampaignName</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartAt</th>
                                
                                
                                
                                <th>EndAt</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>GlobalRiskScore</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {threat_actors.threat_campaigns_threat_actor && Array.isArray(threat_actors.threat_campaigns_threat_actor) &&
                              threat_actors.threat_campaigns_threat_actor.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/threat_campaigns/threat_campaigns-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="start_at">
                                        { dataFormatter.dateTimeFormatter(item.start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="end_at">
                                        { dataFormatter.dateTimeFormatter(item.end_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="global_risk_score">
                                        { item.global_risk_score }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!threat_actors?.threat_campaigns_threat_actor?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Threat_actorsView;