<?php

class ControllerCommonInstaShop extends Controller
{
    public function index()
    {



        //if($this->cache->cache_get($this->session->data['language'] . '_instashop_', 'settings/'))
            //return $this->cache->cache_get($this->session->data['language'] . '_instashop_', 'settings/');




        $this->load->language('extension/module/instashop');
        $store_id = $this->config->get('config_store_id');



        $this->load->model('extension/module/instashop');
        $results = $this->model_extension_module_instashop->getKEY($store_id);

        $apikey = null;
        $urser_info = '';
        $instausername = null;
        if ($results) {
            foreach ($results as $result) {
                if ($result['apikey'] != "") {
                    $apikey = "https://api.instagram.com/v1/users/self/media/recent/?access_token=" . $result['apikey'];
                    $urser_info = "https://api.instagram.com/v1/users/self/?access_token=" . $result['apikey'];
                }
                if ($result['username'] != "") {
                    $instausername = $result['username'];
                }
            }
        }


        $data['instalink'] = "";
        if ($instausername) {
            $data['instalink'] = '<div style="width: 100%; text-align: right"><a href="https://www.instagram.com/' . $instausername . '"><i class="fa fa-instagram"></i> ' . $this->language->get('follow_text') . '</a></div>';
        } else {
            $data['instalink'] = "";
        }


        $data['images'] = array();



        $data['code'] = "";
        if ($apikey) {

            $urser_info = @file_get_contents($urser_info);

            $urser_info = json_decode($urser_info, true);  //var_dump($urser_info); die();
            $data['id'] = $urser_info['data']['id'];
            $data['username'] = $urser_info['data']['username'];
            $data['profile_picture'] = $urser_info['data']['profile_picture'];
            $data['full_name'] = $urser_info['data']['full_name'];
            $data['bio'] = $urser_info['data']['bio'];
            $data['website'] = $urser_info['data']['website'];
            $data['counts'] = $urser_info['data']['counts'];
            $data['media'] = $urser_info['data']['counts']['media'];
            $data['follows'] = $urser_info['data']['counts']['follows'];
            $data['followed_by'] = $urser_info['data']['counts']['followed_by'];

            if (!$jsondata = @file_get_contents($apikey)) {
                $data['codeerror'] = "<div style=\"width: 100%; text-align: center\">Sorry, there seems to be an issue connecting to Instagram right now, please try again later.</div>";
            } else {

                $json = json_decode($jsondata, true);
                if($json['data']){
                    foreach ($json['data'] as $mydata) {
                        $instalink = $mydata['link'];

                        $caption = strtoupper($mydata['caption']['text']);
                        $caption = str_replace("'", "", $caption);
                        $caption = str_replace('"', '', $caption);


                        $datelong = strtoupper($mydata['created_time']);
                        $date = gmdate("d-F-Y", $datelong);
                        $linksarray = array();

                        $img = $mydata['images']['standard_resolution']['url'];
                        $data['images'][] = array(
                            'image' => $mydata['images']['standard_resolution']['url'],
                            'link' => $instalink,
                        );
                    }
                }

            }
        } else {
            $data['code'] = "Please enter your instagram access token in your admin side configuration!";
        }


        $this->cache->cache_set($this->session->data['language'] . '_instashop_', $this->load->view(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') . '/template/common/instashop', $data), 'settings/');
        return $this->cache->cache_get($this->session->data['language'] . '_instashop_', 'settings/');





        /*
        if (file_exists(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') . '/template/common/instashop.tpl')) {
            return $this->load->view(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') . '/template/common/instashop', $data);
        } else {
            return $this->load->view(DIR_TEMPLATE . 'default/template/common/instashop', $data);
        }*/
    }

    public static function instagram(){


        // Supply a user id and an access token
        //Deer case userid&AccessToken
        $userid = "35152f86727f4d018881b11ad7b73eb0";
        $accessToken = "7631084678.35152f8.15b60359f128447e89bdb0fa91f32dfa";

        /* Özgür ÖZKAYA userid&AccessToken
        $userid = "1f546c88789f4f9fb4bdf62e061521b6";
        $accessToken = "695279725.1f546c8.0b6048ac0c1744379e57869d8a48f2bf";
        */
        $timer=5;

        if($timer > 1){
            // Pulls and parses data.
            // Gets our data
            function fetchData($url){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 20);
                $result = curl_exec($ch);
                curl_close($ch);
                return $result;
            }
            $igimages = fetchData("https://api.instagram.com/v1/users/self/media/recent?access_token=$accessToken&count=9");
            $igimages = json_decode($igimages);

            $igUser = fetchData("https://api.instagram.com/v1/users/self/?access_token=$accessToken");
            $igUser = json_decode($igUser);
            //die(var_dump($igUser->meta->count->media));
            var_dump($igimages);
        }else{
            return 1;
        }


        exit;
    }
}

/*
<section class="hg_section pbottom-40" id="_instagram" style="background: #fff; padding-top: 20px; border-top: 2px solid #561819; border-bottom: 2px solid #561819;">
        <div class="hg_section_size container">
            <div class="row">
                <div class="col-md-10 col-md-offset-1 col-sm-12">
                    <div class="col-lg-12" style="padding: 10px;">
                        <div class="row">
                        <div class="col-lg-12 ig_logo"><img src="/upload/logo/instagram2.png" alt="instagram" class="_instagramimg" /></div>
                            <div class="col-lg-10 col-lg-offset-1">
                                <div class="row">
                                    <div class="col-xs-4">
                                        <div class="author-avatar">
                                            <img alt="" src="<?php echo $igUser->profile_picture; ?>" class="avatar avatar-150 photo" height="100" width="100" style="border-radius: 50%;" alt="deercase.com">
                                        </div>
                                    </div>
                                    <div class="col-xs-8" style="margin-bottom: 5px; padding: 0;">
                                        <div class="col-lg-6">
                                            <h3 class="instagram-title" style="margin: 0;"> <strong>@<?php echo $igUser->username; ?></strong> </h3>
</div>
<div class="col-lg-6" style="margin: 10px 0;">
    <a class="igButton" rel="nofollow" href="https://www.instagram.com/<?php echo $igUser->username ?>" target="_blank"><?= Lang::get('go-to-profile') ?></a>
</div>
</div>
<div class="col-xs-12 col-md-8 fright" style="padding: 0;">
    <div class="col-xs-4"><h4 style="margin: 0;"><strong class="instagram-numbers"><?php echo $igUser->media ?></strong> <?= Lang::get('posts') ?></h4></div>
    <div class="col-xs-4"><h4 style="margin: 0;"><strong class="instagram-numbers">
                <?php
                if($igUser->followed_by<100000){ echo substr(number_format($igUser->followed_by), 0, 4)."k"; }
                else{ echo substr($igUser->followed_by, 0, 3)."k"; }
                ?>
            </strong> <?= Lang::get('followers') ?></h4></div>
    <div class="col-xs-4"><h4 style="margin: 0;"><strong class="instagram-numbers"><?php echo $igUser->follows ?></strong> <?= Lang::get('following') ?></h4></div>
</div>
</div>
</div>
<?php foreach ($igimages as $post): $post = (object)$post;?>
    <div class="col-xs-4" style="padding: 0;overflow:hidden;">
        <a title="Deer Case" rel="nofollow" class="gridPhotoGallery__link hoverBorder" href="<?php echo $post->link; ?>" target="_blank">
            <div class="gridPhotoGallery__img igimages"><img src="../../template/images/mobilLogo.png" width="288" height="288" class="resimyukle" alt="telefon kılıfı" style="max-width:288px;max-height:288px;" data-src="<?php echo $post->standard_resolution ?>" /></div>
        </a>
    </div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</section> */