<?php

namespace App\Services;

class CompanyApiService
{
    private $client;
    private $token;
    protected $guzzleService;

    public function __construct()
    {
        $this->guzzleService = new GuzzleHttpService();
    }

    function login()
    {
        $this->client->post('/c/login', [
            'body' => [
                "email" => "admin@company3.com",
                "password" => "43f053d5183be246cdf6",
                "company" => 3
            ]
        ]);
    }
    public function registVoice($data){
        if(isset($data->photo) && $data->photo != null){
            $data->photo = $this->changeUrl($data->photo);
        }
        $response = $this->guzzleService->postRequest('voice/register', $data);

        return $response;
    }

    public function editVoice($id, $data){
        if(isset($data->photo) && $data->photo != null){
            $data->photo = $this->changeUrl($data->photo);
        }

        $response = $this->guzzleService->postRequest('voice/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function deleteVoice($id){
        $response = $this->guzzleService->getRequest('voice/delete/'.$id, []);

        $body = $response;
        return $body;
    }

    public function changeStatusVoice($id, $data){
        $response = $this->guzzleService->postRequest('voice/change/'.$id, $data);
        $body = $response;
        return $body;
    }
    public function changeStatusStaff($id, $data){
        $response = $this->guzzleService->postRequest('staff/change/'.$id, $data);
        $body = $response;
        return $body;
    }
    public function changeStatusStore($id, $data){
        $response = $this->guzzleService->postRequest('store/change/'.$id, $data);
        $body = $response;
        return $body;
    }
    public function changeStatusNews($id, $data){
        $response = $this->guzzleService->postRequest('news/change/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function changeStatusFacility($id, $data){
        if (auth()->user()->company_id === 2){
            return false;
        }
        $response = $this->guzzleService->postRequest('facility/change/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function editHouse($id, $data){
        if ($data->company_id == 3) {
            if($data->floor_file_path != null){
                $data->floor_file_path = $this->changeUrl($data->floor_file_path);
            }
            if($data->file_path2 != null){
                $data->file_path2 = $this->changeUrl($data->file_path2);
            }
        }
        $response = $this->guzzleService->postRequest('house/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function editLand($id, $data){
        if ($data->company_id == 3) {
            if($data->floor_file_path != null){
                $data->floor_file_path = $this->changeUrl($data->floor_file_path);
            }
            if($data->file_path2 != null){
                $data->file_path2 = $this->changeUrl($data->file_path2);
            }
        }
        $response = $this->guzzleService->postRequest('land/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function editRoom($id, $data){
        if ($data->company_id == 3) {
            if($data->floor_file_path != null){
                $data->floor_file_path = $this->changeUrl($data->floor_file_path);
            }
            if($data->file_path2 != null){
                $data->file_path2 = $this->changeUrl($data->file_path2);
            }
        }
        $response = $this->guzzleService->postRequest('room/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function editMansion($id, $data){
        $response = $this->guzzleService->postRequest('mansion/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function editLot($id, $data){
        return $this->guzzleService->postRequest('lot/edit/'.$id, $data);
    }

    public function registImage($id, $data){
        if($data->floor_file_path != null){
            $data->floor_file_path = $this->changeUrl($data->floor_file_path);
        }
        if($data->sheet_file_path2 != null){
            $data->sheet_file_path2 = $this->changeUrl($data->sheet_file_path2);
        }
        if($data->sheet_file_path3 != null){
            $data->sheet_file_path3 = $this->changeUrl($data->sheet_file_path3);
        }
        if($data->file_path2 != null){
            $data->file_path2 = $this->changeUrl($data->file_path2);
        }
        if(!empty($data->tmp_image)){
            foreach ($data->tmp_image as $i => $item){
                $data->tmp_image[$i]->file_path = $this->changeUrl($item->file_path);
            }
        }

        $response = $this->guzzleService->postRequest('image/regist/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function editFacility($id, $data){
        if(isset($data->file_path) && $data->file_path != null){
            $data->file_path = $this->changeUrl($data->file_path);
        }
        if(isset($data->photo1) && $data->photo1 != null){
            $data->photo1 = $this->changeUrl($data->photo1);
        }
        if(isset($data->photo2) && $data->photo2 != null){
            $data->photo2 = $this->changeUrl($data->photo2);
        }
        if(isset($data->photo3) && $data->photo3 != null){
            $data->photo3 = $this->changeUrl($data->photo3);
        }
        if(isset($data->photo4) && $data->photo4 != null){
            $data->photo4 = $this->changeUrl($data->photo4);
        }
        if(isset($data->photo5) && $data->photo5 != null){
            $data->photo5 = $this->changeUrl($data->photo5);
        }
        if(isset($data->photo6) && $data->photo6 != null){
            $data->photo6 = $this->changeUrl($data->photo6);
        }

        $response = $this->guzzleService->postRequest('facility/edit/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function registerNews($data){
        if(isset($data->file_path) && $data->file_path != null){
            $data->file_path = $this->changeUrl($data->file_path);
        }
        $response = $this->guzzleService->postRequest('news/register', $data);
        $body = $response;
        return $body;
    }

    public function editNews($id, $data){
        if(isset($data->file_path) && $data->file_path != null){
            $data->file_path = $this->changeUrl($data->file_path);
        }
        $response = $this->guzzleService->postRequest('news/edit/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function registerReform($data){
        if(isset($data->before_file_path) && $data->before_file_path != null){
            $data->before_file_path = $this->changeUrl($data->before_file_path);
        }
        if(isset($data->after_file_path) && $data->after_file_path != null){
            $data->after_file_path = $this->changeUrl($data->after_file_path);
        }
        if(isset($data['photo_data']) && !empty($data['photo_data'])){
            foreach ($data['photo_data'] as $i => $item){
                $data['photo_data'][$i]->file_path = $this->changeUrl($item->file_path);
            }
        }

        $response = $this->guzzleService->postRequest('reform/register', $data);

        $body = $response;
        return $body;
    }

    public function editReform($id, $data){
        if(isset($data->before_file_path) && $data->before_file_path != null){
            $data->before_file_path = $this->changeUrl($data->before_file_path);
        }
        if(isset($data->after_file_path) && $data->after_file_path != null){
            $data->after_file_path = $this->changeUrl($data->after_file_path);
        }
        if(isset($data['photo_data']) && !empty($data['photo_data'])){
            foreach ($data['photo_data'] as $i => $item){
                $data['photo_data'][$i]->file_path = $this->changeUrl($item->file_path);
            }
        }

        $response = $this->guzzleService->postRequest('reform/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function changeStatusReform($id, $data){
        $response = $this->guzzleService->postRequest('reform/change/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function editStore($id, $data){
        if(isset($data['photo_data']) && !empty($data['photo_data'])){
            foreach ($data['photo_data'] as $i => $item){
                $data['photo_data'][$i]->file_path = $this->changeUrl($item->file_path);
            }
        }

        $response = $this->guzzleService->postRequest('store/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function delStoreImage($id){
        $response = $this->guzzleService->postRequest('store-image/delete', ['id' => $id]);

        $body = $response;
        return $body;
    }

    public function delStaff($id){
        $response = $this->guzzleService->getRequest('staff/delete/'.$id, []);

        $body = $response;
        return $body;
    }

    public function delReform($id){
        $response = $this->guzzleService->getRequest('reform/delete/'.$id, []);

        $body = $response;
        return $body;
    }

    public function delReformImage($id){
        $response = $this->guzzleService->postRequest('reform-image/delete', ['id' => $id]);

        $body = $response;
        return $body;
    }

    public function registHouse($data){
        $response = $this->guzzleService->postRequest('house/register', $data);

        $body = $response;
        return $body;
    }
    public function registLand($data){
        $response = $this->guzzleService->postRequest('land/register', $data);

        $body = $response;
        return $body;
    }

    public function registRoom($data){
        $response = $this->guzzleService->postRequest('room/register', $data);

        $body = $response;
        return $body;
    }

    public function registMansion($data){
        $response = $this->guzzleService->postRequest('mansion/register', $data);

        $body = $response;
        return $body;
    }

    public function registLot($data){
        $response = $this->guzzleService->postRequest('lot/register', $data);

        $body = $response;
        return $body;
    }

    public function registFacility($data){
        if(isset($data->file_path) && $data->file_path != null){
            $data->file_path = $this->changeUrl($data->file_path);
        }
        if(isset($data->photo1) && $data->photo1 != null){
            $data->photo1 = $this->changeUrl($data->photo1);
        }
        if(isset($data->photo2) && $data->photo2 != null){
            $data->photo2 = $this->changeUrl($data->photo2);
        }
        if(isset($data->photo3) && $data->photo3 != null){
            $data->photo3 = $this->changeUrl($data->photo3);
        }
        if(isset($data->photo4) && $data->photo4 != null){
            $data->photo4 = $this->changeUrl($data->photo4);
        }
        if(isset($data->photo5) && $data->photo5 != null){
            $data->photo5 = $this->changeUrl($data->photo5);
        }
        if(isset($data->photo6) && $data->photo6 != null){
            $data->photo6 = $this->changeUrl($data->photo6);
        }

        $response = $this->guzzleService->postRequest('facility/register', $data);
        $body = $response;
        return $body;
    }

    public function saveVendorBusiness($data){
        if ($data->company_id == 3) {
            if($data->floor_file_path != null){
                $data->floor_file_path = $this->changeUrl($data->floor_file_path);
            }
            if($data->file_path2 != null){
                $data->file_path2 = $this->changeUrl($data->file_path2);
            }
        }
        $response = $this->guzzleService->postRequest('save-vendor-business', $data);
        $body = $response;
        return $body;
    }

    public function saveVendorInfo($data){
        return $this->guzzleService->postRequest('save-vendor-info', $data);
    }

    public function registNews(){

    }

    public function registReform(){

    }
    public function registStore(){

    }

    public function deleteStore(){

    }

    public function deleteNews($id){
        $response = $this->guzzleService->getRequest('news/delete/'.$id, []);
        $body = $response;
        return $body;
    }

    public function delArticle($id){
        $response = $this->guzzleService->getRequest('article/delete/'.$id, []);
        $body = $response;
        return $body;
    }
    public function delMansion($id){
        $response = $this->guzzleService->getRequest('mansion/delete/'.$id, []);
        $body = $response;
        return $body;

    }
    public function delLot($id){
        return $this->guzzleService->getRequest('lot/delete/'.$id, []);
    }

    public function delFacility($id){
        $response = $this->guzzleService->getRequest('facility/delete/'.$id, []);
        $body = $response;
        return $body;
    }

    public function registVendor($data)
    {
        if (auth()->user()->company_id === 2){
            return false;
        }
        $response = $this->guzzleService->postRequest('vendor/register', $data);
        $body = $response;
        return $body;
    }

    public function editVendor($id, $data)
    {
        if (auth()->user()->company_id === 2){
            return false;
        }
        $response = $this->guzzleService->postRequest('vendor/edit/' . $id, $data);
        $body = $response;
        return $body;
    }

    public function deleteVendor($id)
    {
        if (auth()->user()->company_id === 2){
            return false;
        }
        $response = $this->guzzleService->getRequest('vendor/delete/' . $id, []);
        $body = $response;
        return $body;
    }

    public function registerStaff($data){
        if(isset($data['user_info']->file_path) && $data['user_info']->file_path != null){
            $data['user_info']->file_path = $this->changeUrl($data['user_info']->file_path);
        }
        if(count($data['custom_item']) > 0){
            foreach ($data['custom_item'] as $i => $item){
                if(isset($item->file_path) && $item->file_path != null){
                    $data['custom_item'][$i]->file_path = $this->changeUrl($item->file_path);
                }
            }
        }

        $response = $this->guzzleService->postRequest('staff/register', $data);

        $body = $response;
        return $body;
    }
    public function editStaff($id, $data){
        if(isset($data['user_info']->file_path) && $data['user_info']->file_path != null){
            $data['user_info']->file_path = $this->changeUrl($data['user_info']->file_path);
        }
        if(count($data['custom_item']) > 0){
            foreach ($data['custom_item'] as $i => $item){
                if(isset($item->file_path) && $item->file_path != null){
                    $data['custom_item'][$i]->file_path = $this->changeUrl($item->file_path);
                }
            }
        }
        $response = $this->guzzleService->postRequest('staff/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function orderStaff($data){
        return  $this->guzzleService->postRequest('staff/order', $data);
    }

    public function saveRecommend($data){
        $response = $this->guzzleService->postRequest('recommend/save', $data);
        $body = $response;
        return $body;
    }

    public function delRecommend($data){
        $response = $this->guzzleService->postRequest('recommend/delete', $data);
        $body = $response;
        return $body;
    }

    public function setRecommendRank($data){
        $response = $this->guzzleService->postRequest('recommend/rank', $data);
        $body = $response;
        return $body;
    }

    public function editReview($id, $data){
        if(isset($data->r_photo) && $data->r_photo != null){
            $data->r_photo = $this->changeUrl($data->r_photo);
        }

        $response = $this->guzzleService->postRequest('review/edit/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function delReview($id){
        $response = $this->guzzleService->getRequest('review/delete/'.$id, []);
        $body = $response;
        return $body;
    }

    public function registLease($data){
        if(isset($data->madori_url) && $data->madori_url != null){
            $data->madori_url = $this->changeUrl($data->madori_url);
        }
        $response = $this->guzzleService->postRequest('lease/register', $data);

        return $response;
    }

    public function editLease($id, $data){
        if(isset($data->madori_url) && $data->madori_url != null){
            $data->madori_url = $this->changeUrl($data->madori_url);
        }
        $response = $this->guzzleService->postRequest('lease/edit/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function setLeaseRank($id, $data){
        $response = $this->guzzleService->postRequest('lease/rank/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function deleteLease($id){
        $response = $this->guzzleService->getRequest('lease/delete/'.$id, []);
        $body = $response;
        return $body;
    }

    public function changeStatusLease($id, $data){
        $response = $this->guzzleService->postRequest('lease/change/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function registImageLease($id, $data){
        if(isset($data->madori_url) && $data->madori_url != null){
            $data->madori_url = $this->changeUrl($data->madori_url);
        }
        if(!empty($data->tmp_image)){
            foreach ($data->tmp_image as $i => $item){
                $data->tmp_image[$i]->photo_url = $this->changeUrl($item->photo_url);
            }
        }

        $response = $this->guzzleService->postRequest('lease-image/regist/'.$id, $data);

        $body = $response;
        return $body;
    }

    public function registTownVoice($data){
        $response = $this->guzzleService->postRequest('town-voice/register', $data);

        return $response;
    }

    public function editTownVoice($id, $data){
        $response = $this->guzzleService->postRequest('town-voice/edit/'.$id, $data);
        $body = $response;
        return $body;
    }


    public function deleteTownVoice($id){
        $response = $this->guzzleService->getRequest('town-voice/delete/'.$id, []);
        $body = $response;
        return $body;
    }

    public function changeStatusTownVoice($id, $data){
        $response = $this->guzzleService->postRequest('town-voice/change/'.$id, $data);
        $body = $response;
        return $body;
    }

    public function changeUrl($url){
        $new_url = env('APP_URL').'/'.$url;
        return $new_url;
    }

    public function registOriginal($data){
        return $this->guzzleService->postRequest('registOriginal', $data);
    }

    public function registCustom($data){
        return $this->guzzleService->postRequest('registCustom', $data);
    }


    public function registCustomVoice($data){
        return $this->guzzleService->postRequest('registCustomVoice', $data);
    }

}
