<?php

class ControllerDevMarketplaceCs extends Controller
{


    private $market;
    private $debug = false;
    private $register = null;
    private $user = null;


    public function delete_attributes()
    {

      $query=  $this->db->query("SELECT * FROM `oc_attribute` where marketplace_id=4 ");

        foreach ($query->rows as $row) {

            $this->db->query("DELETE FROM `oc_attribute_value` where attribute_id='".$row['market_attribute_id']."' ");
            $this->db->query("DELETE FROM `oc_attribute` where market_attribute_id='".$row['market_attribute_id']."' ");
            $this->db->query("DELETE FROM `oc_category_attribute`  where attribute_id='".$row['market_attribute_id']."' ");

        }

    }

    private function check($category)
    {


        $lastItems = explode(' ', $category['name']);
        $sql = "select * from oc_category";

        foreach ($lastItems as $index => $item) {

            if ($index == 0) {
                $sql .= " where name like '%" . $item . "%'";

            } else {

                $sql .= " and name like '%" . $item . "%'";

            }

        }


        $query = $this->db->query($sql);
        if ($query->num_rows == 1) {
            if ($query->row['cs'] == 0) {
                $this->db->query("update oc_category SET `cs`='" . (int)$category['id'] . "' where id='" . $query->row['id'] . "'");

            }

        }

    }


    public function trendcheck($cid)
    {
        //  $check = $this->db->query("SELECT * FROM ".DB_PREFIX."cs_categories WHERE category_id = '".$cid."'");
        return 0;
    }


    public function match_category()
    {


        $url = "https://apis.ciceksepeti.com/api/v1/Categories";


        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//$headers = array();
//$headers["x-api-key"] = "gEeDSWyYgk77M3MMA7mEp23cAi9aA2vMaX9CUxQK";
        $header = array('x-api-key: gEeDSWyYgk77M3MMA7mEp23cAi9aA2vMaX9CUxQK');

        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//for debug only!
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        $resp = curl_exec($curl);
        curl_close($curl);

        $categories = json_decode($resp, true);


        $ix = 0;
        foreach ($categories['categories'] as $trendcat) {
            $name = $trendcat['name'];
            if ($trendcat['subCategories']) {
                foreach ($trendcat['subCategories'] as $trend_1) {
                    $name = $name . ' > ' . $trend_1['name'];
                    if ($trend_1['subCategories']) {
                        foreach ($trend_1['subCategories'] as $trend_2) {
                            $name = $name . ' > ' . $trend_2['name'];
                            if ($trend_2['subCategories']) {
                                foreach ($trend_2['subCategories'] as $trend_3) {
                                    $name = $name . ' > ' . $trend_3['name'];
                                    if ($trend_3['subCategories']) {
                                        foreach ($trend_3['subCategories'] as $trend_4) {
                                            $name = $name . ' > ' . $trend_4['name'];
                                            if ($trend_4['subCategories']) {
                                                foreach ($trend_4['subCategories'] as $trend_5) {
                                                    if ($trend_5['subCategories']) {
                                                        foreach ($trend_5['subCategories'] as $trend_6) {
                                                            if ($this->trendcheck($trend_6['id']) < 1) {
                                                                $ix++;
                                                                $name = $trendcat['name'] . ' > ' . $trend_1['name'] . ' > ' . $trend_2['name'] . ' > ' . $trend_3['name'] . ' > ' . $trend_4['name'] . ' > ' . $trend_5['name'] . ' > ' . $trend_6['name'];
                                                                if (isset($trend_6['parentCategoryId'])) {
                                                                    $parentCategoryId = $trend_6['parentCategoryId'];
                                                                } else {
                                                                    $parentCategoryId = 0;
                                                                }
                                                                //$this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_6['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                                                                $this->check(array('name' => $trend_6['name'], 'id' => $trend_6['id']));

                                                            }
                                                        }
                                                    } else {
                                                        if ($this->trendcheck($trend_5['id']) < 1) {
                                                            $ix++;
                                                            $name = $trendcat['name'] . ' > ' . $trend_1['name'] . ' > ' . $trend_2['name'] . ' > ' . $trend_3['name'] . ' > ' . $trend_4['name'] . ' > ' . $trend_5['name'];
                                                            if (isset($trend_5['parentCategoryId'])) {
                                                                $parentCategoryId = $trend_5['parentCategoryId'];
                                                            } else {
                                                                $parentCategoryId = 0;
                                                            }
                                                            //$this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_5['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                                                            $this->check(array('name' => $trend_5['name'], 'id' => $trend_5['id']));


                                                        }
                                                    }
                                                }
                                            } else {
                                                if ($this->trendcheck($trend_4['id']) < 1) {
                                                    $ix++;
                                                    $name = $trendcat['name'] . ' > ' . $trend_1['name'] . ' > ' . $trend_2['name'] . ' > ' . $trend_3['name'] . ' > ' . $trend_4['name'];
                                                    if (isset($trend_4['parentCategoryId'])) {
                                                        $parentCategoryId = $trend_4['parentCategoryId'];
                                                    } else {
                                                        $parentCategoryId = 0;
                                                    }
                                                    // $this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_4['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                                                    $this->check(array('name' => $trend_4['name'], 'id' => $trend_4['id']));

                                                }
                                            }
                                        }
                                    } else {
                                        if ($this->trendcheck($trend_3['id']) < 1) {
                                            $ix++;
                                            $name = $trendcat['name'] . ' > ' . $trend_1['name'] . ' > ' . $trend_2['name'] . ' > ' . $trend_3['name'];
                                            if (isset($trend_3['parentCategoryId'])) {
                                                $parentCategoryId = $trend_3['parentCategoryId'];
                                            } else {
                                                $parentCategoryId = 0;
                                            }
                                            // $this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_3['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                                            $this->check(array('name' => $trend_3['name'], 'id' => $trend_3['id']));

                                        }
                                    }
                                }
                            } else {
                                if ($this->trendcheck($trend_2['id']) < 1) {
                                    $ix++;
                                    $name = $trendcat['name'] . ' > ' . $trend_1['name'] . ' > ' . $trend_2['name'];
                                    if (isset($trend_2['parentCategoryId'])) {
                                        $parentCategoryId = $trend_2['parentCategoryId'];
                                    } else {
                                        $parentCategoryId = 0;
                                    }
                                    //$this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_2['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                                    $this->check(array('name' => $trend_1['name'], 'id' => $trend_2['id']));

                                }
                            }
                        }
                    } else {
                        if ($this->trendcheck($trend_1['id']) < 1) {
                            $ix++;
                            $name = $trendcat['name'] . ' > ' . $trend_1['name'];
                            if (isset($trend_1['parentCategoryId'])) {
                                $parentCategoryId = $trend_1['parentCategoryId'];
                            } else {
                                $parentCategoryId = 0;
                            }
                            //$this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trend_1['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                            $this->check(array('name' => $trend_1['name'], 'id' => $trend_1['id']));

                        }
                    }
                }
            } else {
                if ($this->trendcheck($trendcat['id']) < 1) {
                    $ix++;
                    $name = $trendcat['name'];
                    if (isset($trendcat['parentCategoryId'])) {
                        $parentCategoryId = $trendcat['parentCategoryId'];
                    } else {
                        $parentCategoryId = 0;
                    }
                    // $this->db->query("INSERT INTO ".DB_PREFIX."cs_categories SET category_id = '".$trendcat['id']."', parent_id = '".$parentCategoryId."', name = '".$name."'");
                    $this->check(array('name' => $name, 'id' => $trendcat['id']));

                }
            }
        }
        return array('status' => 1, 'msg' => $ix . ' Adet Kategori Başarıyla Eşleşti');
    }


    public function category_attributes()
    {

        $marketplace_id = 4;
        $this->load->model('tool/entegrasyon_api');

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {

            $page = 1;
        }

        $token = $this->request->get['user_token'];

        $limit = 1;

        $query = $this->db->query("select category_id from categories_cs limit " . $limit * $page . ",  " . $limit . "  ");

        $request_data = $query->row['category_id'];


        $result = $this->cs->category_attributes($request_data, $this->model_tool_entegrasyon_api->getEntegrasyonApiInfo('cs'), false);


       // print_r($result['result']['categoryAttributes']);return;



        foreach ($result['result']['categoryAttributes'] as $attribute) {


            if (($attribute['type'] == 'Dinamik Özellik') || ($attribute['type'] =='Variant Özelliği') || ($attribute['type'] =='Ürün Özelliği')) {
                $query = $this->db->query("select attribute_id,market_attribute_id from " . DB_PREFIX . "attribute where market_attribute_id='" . $attribute['attributeId'] . "' and marketplace_id='".$marketplace_id."' ");

                if (!$query->num_rows) {

                    $this->db->query("INSERT INTO " . DB_PREFIX . "attribute  SET market_attribute_id ='" . $attribute['attributeId'] . "', marketplace_id='" . $marketplace_id . "', attribute_name='" . $this->db->escape($attribute['attributeName']) . "', required='" . $attribute['required'] . "', varianter='" . $attribute['varianter'] . "'   ");


                  foreach ($attribute['attributeValues'] as $attributeValue) {

                        $this->db->query("INSERT INTO " . DB_PREFIX . "attribute_value SET  attribute_id='" . $attribute['attributeId'] . "', attribute_value_id='" . $attributeValue['id'] . "', `name`='" . $this->db->escape($attributeValue['name']) . "' ");
                    }

                    $attribute_id = $attribute['attributeId'];


                } else {

                    $attribute_id = $query->row['market_attribute_id'];
                }

                $query = $this->db->query("select * from " . DB_PREFIX . "category_attribute where attribute_id='" . $attribute_id . "' and category_id='" . $request_data . "' ");

                $varianter = $attribute['varianter'];


                if (!$query->num_rows) {

                    $this->db->query("INSERT INTO " . DB_PREFIX . "category_attribute SET attribute_id='" . $attribute_id . "', category_id='" . $request_data. "', varianter='" . $varianter . "',required='" . $attribute['required'] . "' ");

                } else {

                    $this->db->query("UPDATE " . DB_PREFIX . "category_attribute SET  varianter='" . $varianter . "', required='" . $attribute['required'] . "' where attribute_id='" . $attribute_id . "' and category_id='" . $request_data . "' ");

                }
                
            }
        }

            $page++;

            echo '<meta http-equiv="refresh" content="0; url=index.php?route=dev/marketplace/cs/category_attributes&user_token=' . $token . '&page=' . $page . '" />';

        }


    }





