<?php
    require_once("./conf.php");
    require_once('ErrorDetail.php');


// localhost/Distributed/Mongo/Auditor/error_detail_data.php?pos=200&p_id=7&error=dup_title&project=flipkart.com
    $pos = "";

    if(isset($_GET['pos'])) {
        $pos = (int)$_GET['pos'];
    }
    if(isset($_GET['error'])) {
        $error = $_GET['error'];
    }
    if(isset($_GET['pages'])) {
        $pages = $_GET['pages'];
    }
    if(isset($_GET['page_limit'])) {
        $limit = (int)$_GET['page_limit'];
    }
    if(isset($_GET['page_offset'])) {
        $offset = $_GET['page_offset'];
    }

    $proj_data = getProjData($my_dist_host, $my_dist_user, $my_dist_pass, $my_dist_db);

    #######################################################
    $ca_id = (int)$proj_data['pauditor_id'] ;
    $pa_id = (int)$proj_data['p2auditor_id'] ;
    #######################################################


    if(defined('SHOW_OLD_WEEK')) {
        $week = (int)$proj_data['crawl_no'] - 2;
    } else {
        $week = (int)$proj_data['crawl_no'] - 1;
    }

    $detail = new ErrorDetail(P_ID,URL_TO_CRAWL,$pos,$ca_id,$pa_id,$limit,$offset);

    if(isset($_GET['action'])) {
        // localhost/Distributed/Mongo/Auditor/error_detail_data.php?p_id=a63ae378ac33596a33f05e9b96b0195b&project=rankwatch.com&pages=250&action=w_failed_urls
        $action = $_GET['action'];
        if($action == "failed_urls") {
            $data = $detail->urlFailed();
        }
        elseif ($action == "unique_urls") {
            $data = $detail->urlUnique();
        }
        elseif ($action == "non200_urls") {
            $data = $detail->urlNon200();
        }
        elseif ($action == "nonindex_urls") {
            $data = $detail->urlNonIndex();
        }
        elseif($action == "w_failed_urls") {
            $data = $detail->urlFailedPast($pages);
        }
        elseif ($action == "w_unique_urls") {
            $data = $detail->urlUniquePast($pages);
        }
        elseif ($action == "w_non200_urls") {
            $data = $detail->urlNon200Past($pages);
        }
        elseif ($action == "w_nonindex_urls") {
            $data = $detail->urlNonIndexPast($pages);
        }
        elseif ($action == 'orphaned_urls') {
            $data = $detail->urlOrphaned();
        }
        elseif ($action == "canonical_urls") {
            $data = $detail->urlCanonical();
        }
        elseif ($action == "redirected_urls") {
            $data = $detail->urlRedirected();
        }
        elseif (array_key_exists($action, $desc_arr)) {
            $data = $detail->urlErrorPast($action, $pages);
        }
        $res_arr = array();
        $res_arr['response'] = $data;
        $res_arr['response_code'] = 100;
        echo json_encode($res_arr);
        exit;
    }
    $get_error_arr = array(
        'mal' => 'missing_alt',
        // 'mkw' => '',
        'lkw' => 'meta_kw_length',
        'skw' => 'meta_kw_length',
        // 'mds' => '',
        'lds' => 'meta_desc_length',
        'sds' => 'meta_desc_length',
        // 'thn' => '',
        // 'low' => '',
        'dti' => 'title_duplicate',
        'dkw' => 'meta_kw_duplicate',
        'dde' => 'meta_desc_duplicate',
        'tu' => 'content_duplicate',
        'lt' => 'title_length',
        'sti' => 'title_length',
        // 'eti' => '',
        // 'mrf' => '',
        // 'mre' => '',
        // 'mnf' => '',
        // 'nin' => '',
        // 'mlg' => '',
        // 'can' => '',
        'mst' => 'sheets_many',
        // 'mh1' => '',
        'eh1' => 'many_h1',
        // 'mh2' => '',
        'eh2' => 'many_h2',
        // 'lu' => 'url_length',
        // 'spu' => '',
        // 'unu' => '',
        // 'upu' => '',
        // 'frm' => '',
        // 'fls' => '',
        'ent' => 'links_int',
        'eex' => 'links_ext',
        // 'nfc' => '',
        'eim' => 'img_many',
        'esp' => 'spell_mistakes',
        'extfp'=> 'external_follow',
        'extnfp'=> 'external_nofollow',
        'intfp'=>'internal_follow',
        'badp'=> 'badres',
        'pgu' => 'pagination_url',
        'bjs' =>'blocked_js',
        'bcss'=>'blocked_css',
        'ogp'=> 'open_graph',
        'twtp'=>'twitter_card',
        'invtc'=>'twitter_card'
    );

    if(array_key_exists($error, $get_error_arr))
    {
        $res = $detail->$get_error_arr[$error]();
    }

    $res_arr = array();
    $res_arr['response'] = $res;
    $res_arr['response_code'] = 100;
    echo json_encode($res_arr);
?>
