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/storage_objects/storage_objectsSlice'
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 Storage_objectsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { storage_objects } = useAppSelector((state) => state.storage_objects)
    
    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 storage_objects')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View storage_objects')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/storage_objects/storage_objects-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Bucket</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{storage_objects?.bucket?.bucket_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ObjectKey</p>
                    <p>{storage_objects?.object_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContentType</p>
                    <p>{storage_objects?.content_type}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SizeBytes</p>
                  <p>{storage_objects?.size_bytes || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ETag</p>
                    <p>{storage_objects?.etag}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SHA256</p>
                    <p>{storage_objects?.sha256}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PreviewFile</p>
                    {storage_objects?.preview_file?.length
                      ? dataFormatter.filesFormatter(storage_objects.preview_file).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No PreviewFile</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>OriginalFile</p>
                    {storage_objects?.original_file?.length
                      ? dataFormatter.filesFormatter(storage_objects.original_file).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No OriginalFile</p>
                    }
                </div>
              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CompressionRatio</p>
                  <p>{storage_objects?.compression_ratio || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{storage_objects?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Media_items StorageObject</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>MediaType</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                
                                
                                <th>CapturedAt</th>
                                
                                
                                
                                <th>UploadedAt</th>
                                
                                
                                
                                <th>Width</th>
                                
                                
                                
                                <th>Height</th>
                                
                                
                                
                                <th>DurationSeconds</th>
                                
                                
                                
                                <th>CameraMake</th>
                                
                                
                                
                                <th>CameraModel</th>
                                
                                
                                
                                <th>GPSLatitude</th>
                                
                                
                                
                                <th>GPSLongitude</th>
                                
                                
                                
                                <th>Visibility</th>
                                
                                
                                
                                <th>IsDuplicate</th>
                                
                                
                                
                                <th>DuplicateGroupKey</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {storage_objects.media_items_storage_object && Array.isArray(storage_objects.media_items_storage_object) &&
                              storage_objects.media_items_storage_object.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/media_items/media_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="media_type">
                                        { item.media_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="captured_at">
                                        { dataFormatter.dateTimeFormatter(item.captured_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="uploaded_at">
                                        { dataFormatter.dateTimeFormatter(item.uploaded_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="width">
                                        { item.width }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="height">
                                        { item.height }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="camera_make">
                                        { item.camera_make }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="camera_model">
                                        { item.camera_model }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gps_latitude">
                                        { item.gps_latitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gps_longitude">
                                        { item.gps_longitude }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="visibility">
                                        { item.visibility }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_duplicate">
                                        { dataFormatter.booleanFormatter(item.is_duplicate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duplicate_group_key">
                                        { item.duplicate_group_key }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!storage_objects?.media_items_storage_object?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Storage_objectsView;