<?php

/**
 *
 */
class nc_services_direct_admin_controller extends nc_services_admin_controller
{

    /** @var  nc_services_direct_admin_ui */
    protected $ui_config;
    protected $ui_config_class = 'nc_services_direct_admin_ui';

    /**
     *
     */
    protected function init()
    {
        parent::init();
    }

    protected function view($view, $data = array())
    {
        $view = "direct/" . $view;
        $view = parent::view($view, $data)
          ->with('link_prefix', nc_core('SUB_FOLDER') . '/netcat/admin/#module.services.direct');
        return $view;
    }

    /**
     *
     */
    protected function before_action()
    {
        $this->campaign_id = (int) $this->input->fetch_post_get('campaign_id');

        $this->ui_config = new nc_services_direct_admin_ui(
          $this->current_action
        );

        if (empty($this->services->direct->auth_token)) {
            echo parent::view('empty_error')->with('message', constant("NETCAT_MODULE_SERVICES_EMPTY_TOKEN"));
        }
        if (empty($this->services->direct->direct_login)) {
            echo parent::view('empty_error')->with('message', constant("NETCAT_MODULE_SERVICES_DIRECT_LOGIN_EMPTY"));
        }
    }

    protected function action_moderate_campaign()
    {
        $banners = $this->services->direct->banner->get_list($this->campaign_id);
        if (count($banners) < 1 && $this->services->direct->banner->api->error != "") {
            $view = parent::view('empty_error')->with('message', $this->services->direct->banner->api->error);
        } else if (count($banners) == 0) {
            $message = constant("NETCAT_MODULE_SERVICES_DIRECT_NO_BANNERS");
            $view = parent::view('empty_list')->with('message', $message);
        } else {
            $result = $this->services->direct->banner->send_moderate($banners, $this->campaign_id);
            if (!empty($result['error'])) {
                echo parent::view('empty_error')->with('message', $result['error']);
            } else {
                parent::redirect_to_index_action();
            }
        }
    }

    protected function action_edit_campaign()
    {
        $campaign = array();
        if (!empty($this->campaign_id)) {
            //get
            $result = $this->services->direct->campaign->get($this->campaign_id);
            if (!empty($result['error'])) {
                echo parent::view('empty_error')->with('message', $result['error']);
            } else {
                $campaign = $result;
            }
        }

        $strategies = array();
        $strategies_fields = array();
        foreach ($this->services->direct->campaign->show_strategies as $strategy) {
            $strategies[$strategy] = constant(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_STRATEGY_ . strtoupper($strategy));
            if (isset($this->services->direct->campaign->strategies_fields[$strategy])) {
                foreach ($this->services->direct->campaign->strategies_fields[$strategy] as $name => $requirement) {
                    $strategies_fields[$strategy][$name] = array('key' => $name, 'name' => constant(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_STRATEGY_ . strtoupper($name)));
                }
            }
        }
        $view = $this->view('campaign_edit')
          ->with('campaign', $campaign)
          ->with('strategies', $strategies)
          ->with('strategies_fields', $strategies_fields)
          ->with('campaign_id', !empty($this->campaign_id) ? $this->campaign_id : 0)
        ;

        $this->ui_config->add_save_and_cancel_buttons();
        $this->ui_config->locationHash .=
          ($this->campaign_id ? ".campaign.edit({$this->campaign_id})" : ".campaign.add"
          );

        return $view;
    }

    protected function action_edit_banner()
    {
        $banner = array();
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        if (!empty($banner_id)) {
            //get
            $result = $this->services->direct->banner->get($banner_id);
            if (!empty($result['error'])) {
                echo parent::view('empty_error')->with('message', $result['error']);
            } else {
                $banner = $result;
            }
        }
        $view = $this->view('banner_edit')
          ->with('banner', $banner)
          ->with('regions', $this->services->direct->banner->get_regions())
          ->with('banner_id', !empty($banner_id) ? $banner_id : 0)
          ->with('campaign_id', empty($banner_id) ? $this->campaign_id : $banner['CampaignID'])
          ->with('js_path', nc_module_path('services') . "js/")
        ;


        $this->ui_config->add_save_and_cancel_buttons();
        $this->ui_config->locationHash .=
          ($banner_id ? ".banner.edit({$banner_id})" : ".banner.add({$this->campaign_id})"
          );
        return $view;
    }

    /**
     * @return nc_ui_view
     */
    protected function action_index()
    {
        $add_link = "direct.campaign.add";
        $this->ui_config->add_create_button($add_link);

        $campaigns = $this->services->direct->campaign->get_list();
        if (count($campaigns) < 1 && $this->services->direct->campaign->api->error != "") {
            $view = parent::view('empty_error')->with('message', $this->services->direct->campaign->api->error);
        } else if (count($campaigns) == 0) {
            $message = constant("NETCAT_MODULE_SERVICES_DIRECT_NO_CAMPAIGNS");
            $view = parent::view('empty_list')->with('message', $message);
        } else {
            $view = $this->view('campaigns_list')
              ->with('campaigns', $campaigns);
        }
        return $view;
    }

    /**
     * @return nc_ui_view
     */
    protected function action_banner_list()
    {
        $this->ui_config->_action = "banner.list";
        $this->ui_config->locationHash .= ".banner.list($this->campaign_id)";

        $add_link = "direct.banner.add($this->campaign_id)";
        $this->ui_config->add_create_button($add_link);

        $banners = $this->services->direct->banner->get_list($this->campaign_id);
        if (count($banners) < 1 && $this->services->direct->banner->api->error != "") {
            $view = parent::view('empty_error')->with('message', $this->services->direct->banner->api->error);
        } else if (count($banners) == 0) {
            $message = constant("NETCAT_MODULE_SERVICES_DIRECT_NO_BANNERS");
            $view = parent::view('empty_list')->with('message', $message);
        } else {
            $view = $this->view('banners_list')
              ->with('banners', $banners)
              ->with('campaign_id', $this->campaign_id);
        }
        return $view;
    }

    protected function action_save_campaign()
    {
        $data = (array) $this->input->fetch_post('data');
        $result = $this->services->direct->campaign->save($data);
        if (!empty($result['error'])) {
            echo parent::view('empty_error')->with('message', $result['error']);
        } else {
            parent::redirect_to_index_action();
        }
    }

    protected function action_save_banner()
    {
        $data = (array) $this->input->fetch_post('data');
        $result = $this->services->direct->banner->save($data);
        if (!empty($result['error'])) {
            echo parent::view('empty_error')->with('message', $result['error']);
        } else {
            parent::redirect_to_index_action();
        }
    }

    protected function action_remove()
    {

        $id = (int) $this->input->fetch_get_post('campaign_id');
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        if ($banner_id > 0) {
            $result = $this->services->direct->banner->remove($banner_id, $id);
        } else {
            $result = $this->services->direct->campaign->remove($id);
        }
        if (!empty($result['error'])) {
            echo parent::view('empty_error')->with('message', $result['error']);
        } else {
            parent::redirect_to_index_action();
        }
    }

    protected function action_stop_campaign()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $this->services->direct->campaign->stop($id);
        parent::redirect_to_index_action();
    }

    protected function action_stop_banner()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        $this->services->direct->banner->stop($banner_id, $id);
        parent::redirect_to_index_action('banner_list', 'campaign_id=' . $id);
    }

    protected function action_resume_campaign()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $this->services->direct->campaign->resume($id);
        parent::redirect_to_index_action();
    }

    protected function action_resume_banner()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        $this->services->direct->banner->resume($banner_id, $id);
        parent::redirect_to_index_action('banner_list', 'campaign_id=' . $id);
    }

    protected function action_archive_campaign()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $this->services->direct->campaign->archive($id);
        parent::redirect_to_index_action();
    }

    protected function action_archive_banner()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        $this->services->direct->banner->archive($banner_id, $id);
        parent::redirect_to_index_action('banner_list', 'campaign_id=' . $id);
    }

    protected function action_unarchive_campaign()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $this->services->direct->campaign->unarchive($id);
        parent::redirect_to_index_action();
    }

    protected function action_unarchive_banner()
    {
        $id = (int) $this->input->fetch_get_post('campaign_id');
        $banner_id = (int) $this->input->fetch_get_post('banner_id');
        $this->services->direct->banner->unarchive($banner_id, $id);
        parent::redirect_to_index_action('banner_list', 'campaign_id=' . $id);
    }
    
    protected function action_add_funds_campaign()
    {

        $view = $this->view('campaign_add_funds')
              ->with('campaign_id', $this->campaign_id);

        $this->ui_config->_action = "campaign.add_funds";
        $this->ui_config->locationHash .= ".campaign.add_funds($this->campaign_id)";
        $this->ui_config->add_save_and_cancel_buttons();
        
        return $view;        
    }

    protected function action_send_funds_campaign()
    {
        $data = (array) $this->input->fetch_post('data');
        $result = $this->services->direct->campaign->add_funds($data);
        if (!empty($result['error'])) {
            echo parent::view('empty_error')->with('message', $result['error']);
        } else {
            header('Location: '.$result['data']);
        }
    }
}
