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/assets/assetsSlice'
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 AssetsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { assets } = useAppSelector((state) => state.assets)
    

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

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


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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Asset format</p>
                    <p>{assets?.asset_type ?? 'No data'}</p>
                </div>
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Type</p>
                    <p>{assets?.type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CDNURL</p>
                    <p>{assets?.cdn_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Storagekey</p>
                    <p>{assets?.storage_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>MIMEtype</p>
                    <p>{assets?.mime_type}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Size(MB)</p>
                  <p>{assets?.size_mb || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Width(px)</p>
                  <p>{assets?.width_px || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Height(px)</p>
                  <p>{assets?.height_px || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Duration(sec)</p>
                  <p>{assets?.duration_sec || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Checksum</p>
                    <p>{assets?.checksum}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Ispublic'>
                    <SwitchField
                      field={{name: 'is_public', value: assets?.is_public}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Isdeleted'>
                    <SwitchField
                      field={{name: 'is_deleted', value: assets?.is_deleted}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Asset_variants Asset</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Varianttype</th>
                                
                                
                                
                                <th>CDNURL</th>
                                
                                
                                
                                <th>Width(px)</th>
                                
                                
                                
                                <th>Height(px)</th>
                                
                                
                                
                                <th>Size(MB)</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {assets.asset_variants_asset && Array.isArray(assets.asset_variants_asset) &&
                              assets.asset_variants_asset.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/asset_variants/asset_variants-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="variant_type">
                                        { item.variant_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cdn_url">
                                        { item.cdn_url }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="width_px">
                                        { item.width_px }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="height_px">
                                        { item.height_px }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="size_mb">
                                        { item.size_mb }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!assets?.asset_variants_asset?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default AssetsView;
