<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\Ebook;
use App\Models\EbookVolume;
use App\Models\Issue;
use App\Models\Journal;
use App\Models\MetaData;
use App\Models\Volume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class MetaDataController extends BenthamController
{
    public function getArticleMetaData(Request $request, $journal_id = null, $volume_id = null, $issue_id = null, $article_id = null, $type = null, $is_epub = null)
    {
        $param['journal_id']="";
        $param['volume_id']="";
        $param['issue_id'] ="";
        $param['is_epub']="";
        
        if (!empty($_POST)) {
            $articles            = '';
            $param['journal_id'] = $journal_id = $request->get('journal_id');
            $param['volume_id']  = $volume_id  = $request->get('volume_id');
            $param['issue_id']   = $issue_id   = $request->get('issue_id');
            $param['is_epub']    = $is_epub    = $request->get('section_id');

            $journal        = Journal::GetJournalTitle();
			
			$param_j['journal_id'] = $journal_id;
            $volume         = Volume::GetVolumeTitleById($param_j);
            $p['volume_id'] = $volume_id;
            $issue          = Issue::GetIssueTitleById($p);

            $articles_list = Article::GetJournalArticleList($param);
            return View::make('admin.metadata.articlelist', with(['journal' => $journal,
             'volume' => $volume,
              'issue' => $issue,
               'articles_list' => $articles_list,
                'param' => $param]));

        } elseif (!empty($type)) {

            $p['journal_id'] = $journal_id;
            $p['volume_id']  = $volume_id;
            $p['issue_id']   = $issue_id;
            $p['article_id'] = $article_id;
            $p['is_epub']    = $is_epub;
            $ArticleMetaData = MetaData::GetJournalArticleMetadata($p);
            $ArticleMetaData = $ArticleMetaData[0];

            switch ($type) {

                case "1":
                    include 'Metadata/ArticleMetaData/CCCMetaData.php';
                    $CCCMetaData = new CCCMetaData();
                    $CCCMetaData->GetCCCMetaData($ArticleMetaData);
                    break;
                case "2":
                    include 'Metadata/ArticleMetaData/EpubAbstractMetaData.php';
                    $EpubAbstractMetaData = new EpubAbstractMetaData();
                    $EpubAbstractMetaData->GetEpubAbstractMetaData($ArticleMetaData); // Metadata for Epub
                    break;
                case "3":
                    include 'Metadata/ArticleMetaData/EpubAbstractPubMedLinkoutMetaData.php';
                    $EpubAbstractPubMedLinkoutMetaData = new EpubAbstractPubMedLinkoutMetaData();
                    $EpubAbstractPubMedLinkoutMetaData->GetEpubAbstractPubMedLinkout($ArticleMetaData); // Epub Abstract Linkout file for PubMed
                    break;
                case "4":
                    include 'Metadata/ArticleMetaData/EpubAbstractCrossRefDOIMetaData.php';
                    $EpubAbstractCrossRefDOIMetaData = new EpubAbstractCrossRefDOIMetaData();
                    $EpubAbstractCrossRefDOIMetaData->GetEpubAbstractCrossRefDOI($ArticleMetaData); // Epub Abstract Linkout file for CrossRef
                    break;
                case "5":
                    include 'Metadata/ArticleMetaData/JSTMetaData.php';
                    $JSTArticleMetaData = MetaData::GetJSTMetaData($p);
                    
                    $JSTMetaData = new JSTMetaData();
                    $JSTMetaData->GetJSTMetaData($JSTArticleMetaData);
                    break;
                case "6":
                    include 'Metadata/ArticleMetaData/InfotriveEpubMetaData.php';
                    $InfotriveEpubMetaData = new InfotriveEpubMetaData();
                    $InfotriveEpubMetaData->GetInfotriveEpub($ArticleMetaData); // JST
                    break;
                case "7":
                    include 'Metadata/ArticleMetaData/TIBMetaData.php';
                    $TIBArticleMetaData = MetaData::GetJSTMetaData($p);//same as JST, so do not need to create extra model function
                    
                    $TIBMetaData = new TIBMetaData();
                    $TIBMetaData->GetTIBMetaData($TIBArticleMetaData);
                    break;
                case "8":
                    include 'Metadata/ArticleMetaData/CrossRef.php';
                    $CrossRefMetaData = new CrossRef();
                    $CrossRefMetaData->GetCrossRef($ArticleMetaData);
                    break;                case "9":
                    include 'Metadata/ArticleMetaData/JatsNormalMetaData.php';
                    $GetJatsNormalData = new JatsNormalMetaData();
                    $GetJatsNormalData->GetJatsNormalMetaData($ArticleMetaData);
                    break;
                case "10":
                    include 'Metadata/ArticleMetaData/JatsEpubMetaData.php';
                    $GetJatsEpubMetaData = new JatsEpubMetaData();
                    $GetJatsEpubMetaData->GetJatsEpubMetaData($ArticleMetaData);
                    break;
                case "11":
                    include 'Metadata/ArticleMetaData/NormalMedlineMetaData.php';
                    $NormalMedlineMetaData = new NormalMedlineEpubMetaData();
                    $NormalMedlineMetaData->GetNormalMedlineMetaData($ArticleMetaData);
                    break;
				case "12":
                    include 'Metadata/ArticleMetaData/EcoVectorTech.php';
                    $GetEcoVectorTechData = new EcoVectorTech();
                    $GetEcoVectorTechData->GetEcoVectorTechData($ArticleMetaData);
                    break;
            } //switch

        } else {
            $journal_id = $request->get('journal_id');

            if (!empty($journal_id)) {
                $param['journal_id'] = $journal_id;
                $volume = Volume::GetVolumeTitleById($param);
                return response()->json(array('volume' => $volume), 200);
            }

            $volume_id = $request->get('volume_id');

            if (!empty($volume_id)) {
                $param['volume_id'] = $volume_id;
                $issues             = Issue::GetIssueTitleById($param);
                return response()->json(array('issues' => $issues), 200);
            }

            $journal = Journal::GetJournalTitle();
            $volume  = array();
            $issue   = array();
            $articles_list = array();
            return View::make('admin.metadata.articlelist', with(['journal' => $journal,
             'volume' => $volume,
              'issue' => $issue,
              'articles_list' => $articles_list,
               'param' => $param]));
        }

    }

    public function getIssueMetaData(Request $request, $journal_id = null, $volume_id = null, $issue_id = null, $type = null)
    {
        $param=array();
        if (!empty($_POST)) {

            $articles            = '';
            $param['journal_id'] = $journal_id = $request->get('journal_id');
            $param['volume_id']  = $volume_id  = $request->get('volume_id');
            // $param['issue_id']  = $issue_id = $request->get('type');

            $journal    = Journal::GetJournalTitle();
			
			$param_j['journal_id'] = $journal_id;
            $volume     = Volume::GetVolumeTitleById($param_j);
            $issue_list = Issue::GetIssueById($param);

           
            // $articles = Article::GetJournalArticleList($param);
            return View::make('admin.metadata.issuelist', with(['journal' => $journal,
             'volume' => $volume,
              'issue_list' => $issue_list,
               'param' => $param]));

        } elseif (!empty($type)) {

            
            $p['journal_id'] = $journal_id;
            $p['volume_id']  = $volume_id;
            $p['issue_id']   = $issue_id;

            $ArticleMetaData = MetaData::GetJournalIssueMetadata($p);
            // $ArticleMetaData = $ArticleMetaData[0];

            if ($type == 1) {
                include 'Metadata/IssueMetaData/Metadata1.php';
                $Metadata1 = new Metadata1();
                $Metadata1->GetMetadata1($ArticleMetaData);
            }
            if ($type == 2) {
                include 'Metadata/IssueMetaData/Metadata2.php';
                $Metadata2 = new Metadata2();
                $Metadata2->GetMetadata2($ArticleMetaData);
            }
            if ($type == 3) {
                include 'Metadata/IssueMetaData/CrossRef.php';
                $CrossRef = new CrossRef();
                $CrossRef->GetCrossRef($ArticleMetaData);
            }
            if ($type == 4) {
                include 'Metadata/IssueMetaData/Medline.php';
                $Medline = new Medline();
                $Medline->GetMedlineLinkout($ArticleMetaData);
            }
            if ($type == 5) {
                include 'Metadata/IssueMetaData/FIZAutodoc.php';
                $FIZAutodoc = new FIZAutodoc();
                $FIZAutodoc->GetFIZAutodocMetadata($ArticleMetaData);
            }
            if ($type == 6) {
                include 'Metadata/IssueMetaData/ExLibras.php';
                $ExLibras = new ExLibras();
                $ExLibras->GetExLibrasMetadata($ArticleMetaData);
            }
            if ($type == 7) {
                include 'Metadata/IssueMetaData/MedlineMetaData.php';
                $MedlineMetaData = new MedlineMetaData();
                $MedlineMetaData->GetMedlineMetaData($ArticleMetaData);
            }
            if ($type == 8) {
                include 'Metadata/IssueMetaData/Onix.php';
                $Onix = new Onix();
                $Onix->GetOnix($ArticleMetaData);
            }
            if ($type == 9) {
                include 'Metadata/IssueMetaData/Nlst.php';
                $Nlst = new Nlst();
                $Nlst->GetNlst($ArticleMetaData);
            }
        } else {
			
            $journal_id = $request->get('journal_id');

            if (!empty($journal_id)) 
			{
				$param_j['journal_id'] = $journal_id;
                $volume = Volume::GetVolumeTitleById($param_j);
                return response()->json(array('volume' => $volume), 200);
            }

            $volume_id = $request->get('volume_id');

            if (!empty($volume_id)) {
                $param['volume_id'] = $volume_id;
                $issues             = Issue::GetIssueTitleById($param);
                return response()->json(array('issues' => $issues), 200);
            }

            $journal = Journal::GetJournalTitle();
            $volume  = array();
            $issue_list  = array();
            return View::make('admin.metadata.issuelist', with(['journal' => $journal,
             'volume' => $volume,
             'issue_list' => $issue_list,
              'param' => $param]));
        }

    }

    public function getEbookMetaData(Request $request, $ebook_id = null, $volume_id = null, $type = null)
    {  
        $param=array();
        if (!empty($_POST)) {

            $param['ebook_id'] = $ebook_id = $request->get('ebook_id');

            $ebook_list = EbookVolume::GetVolumesByEbookId($ebook_id);

            $ebook = Ebook::GetEbookTitle();
            return View::make('admin.metadata.ebooklist', with(['ebook' => $ebook,
             'ebook_list' => $ebook_list,
              'param' => $param]));

        } elseif (!empty($type)) {

            $param['ebook_id']  = $ebook_id;
            $param['volume_id'] = $volume_id;

            $ebook_list = MetaData::GetEbookMetadata($param);

            if ($type == 1) {
                include 'Metadata/EbookMetaData/EbookMedline.php';
                $Medline = new EbookMedline();
                $Medline->GetMedline($ebook_list);
            }
            if ($type == 2) {
                include 'Metadata/EbookMetaData/EbookCrossRef.php';
                $CrossRef = new EbookCrossRef();
                $CrossRef->GetCrossRef($ebook_list);
            }

            if ($type == 3) {
                include 'Metadata/EbookMetaData/EbookOnix.php';
                $Onix = new EbookOnix();
                $Onix->GetOnix($ebook_list);
            }

            if ($type == 4) {
                include 'Metadata/EbookMetaData/Series.php';
                $Series = new Series();
                $Series->GetSeries($ebook_list);
            }

            if ($type == 5) {
                include 'Metadata/EbookMetaData/EBSCO.php';
                $EBSCO = new EBSCO();
                $EBSCO->GetEBSCO($ebook_list);
            }

            if ($type == 6) {
                include 'Metadata/EbookMetaData/KOBO.php';
                $KOBO = new KOBO();
                $KOBO->GetKOBO($ebook_list);
            }

            if ($type == 7) {
                include 'Metadata/EbookMetaData/Infotrive.php';
                $Infotrive = new Infotrive();
                $Infotrive->GetInfotrive($ebook_list);
            }
            if ($type == 8) {
                include 'Metadata/EbookMetaData/TIB.php';
                $TIB = new TIB();
                $TIB->GetTIB($ebook_list);
            }
			if ($type == 9) {
                $subject_list = MetaData::GetSubjectTitlebyEbookVolumeId($ebook_id);
                include 'Metadata/EbookMetaData/Scopus.php';
                $Scopus = new Scopus();
                $Scopus->GetScopus($ebook_list, $subject_list);
            }
        } else {
            $ebook = Ebook::GetEbookTitle();
            $ebook_list = array();
            return View::make('admin.metadata.ebooklist', with(['ebook' => $ebook, 'ebook_list' => $ebook_list, 'param' => $param]));
        }

    }

    public function getEbookBentsponixMetaData(Request $request)
    {  
       $param=array();
        if (!empty($_POST)) {

            $from = explode('-', $request->get('from'));
            $to = explode('-', $request->get('to'));

            $param['from'] = $from[0]."-".$from[1]."-01";
            $to = $to[0]."-".$to[1]."-28";
            $param['to'] = date('Y-m-t', strtotime($to));

            $ebook_list = EbookVolume::GetEbookVolume($param);

            include 'Metadata/EbookMetaData/BENTSPONIX.php';
            $BENTSPONIX = new BENTSPONIX();
            $BENTSPONIX->GetBENTSPONIX($ebook_list);

        } 
        else {
            return View::make('admin.metadata.ebook_bentsponix');
        }

    }
    public function index()
    {

        return View::make('admin.metadata.index');
    }

}
