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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

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

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CreatedBy</p>
                    
                    
                        <p>{projects?.created_user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TargetBPM</p>
                  <p>{projects?.target_bpm || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TargetKey</p>
                    <p>{projects?.target_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Mood</p>
                    <p>{projects?.mood ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TargetDurationSeconds</p>
                  <p>{projects?.target_duration_seconds || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='CollaborationEnabled'>
                    <SwitchField
                      field={{name: 'collaboration_enabled', value: projects?.collaboration_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Project_collaborations Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>PermissionLevel</th>
                                
                                
                                
                                <th>InvitedAt</th>
                                
                                
                                
                                <th>AcceptedAt</th>
                                
                                
                                
                                <th>InviteStatus</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.project_collaborations_project && Array.isArray(projects.project_collaborations_project) &&
                              projects.project_collaborations_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/project_collaborations/project_collaborations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="permission_level">
                                        { item.permission_level }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invited_at">
                                        { dataFormatter.dateTimeFormatter(item.invited_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="accepted_at">
                                        { dataFormatter.dateTimeFormatter(item.accepted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="invite_status">
                                        { item.invite_status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.project_collaborations_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Songs Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SongTitle</th>
                                
                                
                                
                                <th>ArtistDisplay</th>
                                
                                
                                
                                
                                
                                <th>BPM</th>
                                
                                
                                
                                <th>KeySignature</th>
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>DurationSeconds</th>
                                
                                
                                
                                
                                
                                <th>ExplicitContent</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.songs_project && Array.isArray(projects.songs_project) &&
                              projects.songs_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/songs/songs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="artist_display">
                                        { item.artist_display }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="bpm">
                                        { item.bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="key_signature">
                                        { item.key_signature }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="explicit_content">
                                        { dataFormatter.booleanFormatter(item.explicit_content) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.songs_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Assets Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>AssetType</th>
                                
                                
                                
                                <th>AudioRole</th>
                                
                                
                                
                                <th>AssetName</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>DurationSeconds</th>
                                
                                
                                
                                <th>SampleRateHz</th>
                                
                                
                                
                                <th>BitDepth</th>
                                
                                
                                
                                <th>IsStereo</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.assets_project && Array.isArray(projects.assets_project) &&
                              projects.assets_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/assets/assets-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="asset_type">
                                        { item.asset_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="audio_role">
                                        { item.audio_role }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="duration_seconds">
                                        { item.duration_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sample_rate_hz">
                                        { item.sample_rate_hz }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bit_depth">
                                        { item.bit_depth }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_stereo">
                                        { dataFormatter.booleanFormatter(item.is_stereo) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.assets_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Recording_sessions Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>EndedAt</th>
                                
                                
                                
                                <th>InputChannels</th>
                                
                                
                                
                                <th>SampleRateHz</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.recording_sessions_project && Array.isArray(projects.recording_sessions_project) &&
                              projects.recording_sessions_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/recording_sessions/recording_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended_at">
                                        { dataFormatter.dateTimeFormatter(item.ended_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="input_channels">
                                        { item.input_channels }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sample_rate_hz">
                                        { item.sample_rate_hz }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.recording_sessions_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Generation_requests Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>RequestType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>TargetBPM</th>
                                
                                
                                
                                <th>TargetKey</th>
                                
                                
                                
                                <th>Mood</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>QueuedAt</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>ComputeSeconds</th>
                                
                                
                                
                                <th>EstimatedCost</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.generation_requests_project && Array.isArray(projects.generation_requests_project) &&
                              projects.generation_requests_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/generation_requests/generation_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="request_type">
                                        { item.request_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="target_bpm">
                                        { item.target_bpm }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="target_key">
                                        { item.target_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mood">
                                        { item.mood }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="queued_at">
                                        { dataFormatter.dateTimeFormatter(item.queued_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="compute_seconds">
                                        { item.compute_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_cost">
                                        { item.estimated_cost }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.generation_requests_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default ProjectsView;