package com.cloudcross.ssp.web.advertiser.main.ad.target;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.Location;
import com.cloudcross.ssp.model.Operator;
import com.cloudcross.ssp.model.Place;
import com.cloudcross.ssp.model.PlaceClassInformation;
import com.cloudcross.ssp.model.PlaceOperator;
import com.cloudcross.ssp.model.Wifi;
import com.cloudcross.ssp.model.pojo.CampaignPojo;
import com.cloudcross.ssp.service.ICampaignService;
import com.cloudcross.ssp.service.ILocationService;
import com.cloudcross.ssp.service.IOperatorService;
import com.cloudcross.ssp.service.IPlaceClassInformationService;
import com.cloudcross.ssp.service.IPlaceOperatorService;
import com.cloudcross.ssp.service.IPlaceService;
import com.cloudcross.ssp.service.IWifiService;

@Controller
@RequestMapping("/advertiser/main/ad/target/ad-group-alloperator")
public class AdGroupAlloperatorController extends SimpleController{
	
	@Autowired
	private ICampaignService campaignService;
	@Autowired
	private ILocationService locationService;
	@Autowired
	private IOperatorService operatorService;
	@Autowired
	private IPlaceOperatorService placeOperatorService;
	@Autowired
	private IPlaceClassInformationService placeClassInformationService;
	@Autowired
	private IPlaceService placeService;
	@Autowired
	private IWifiService wifiService;
	
	@RequestMapping("create")
	public String create(Model model, @RequestParam Long campaignId) {
		Account account = getLoginUser();
		Long operatorId = account.getOperatorId();
		
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("operatorId", operatorId);
		List<Integer> positionList = new ArrayList<Integer>();
		positionList.add(1);
		condition.put("positionList", positionList);
		//List<Zone> zoneList = zoneService.queryZoneByCondition(condition);//获取广告位
		CampaignPojo campaign = campaignService.findCampaignById(campaignId);
		//找到所有的场景，并且显示出来
		//List<PlaceClassInformation> placeList = placeClassInformationService.findAll();
		
		
		model.addAttribute("campaignId", campaignId);
		model.addAttribute("advertiserId", campaign.getAdvertiserId());	
		model.addAttribute("orderId", campaign.getOrderId());
		model.addAttribute("agentId", campaign.getAgentId());
		model.addAttribute("budget", campaign.getTotalBudget());
		//model.addAttribute("placeList",placeList);
		//model.addAttribute("zoneList",zoneList);
		return page("create");
	}
	
	/**
	 * 到选择媒体弹窗
	 * 创建人：周俊
	 * 创建时间：2015.12.11 17：00
	 * @return
	 */
	@RequestMapping("chooseOperator")
	@ResponseBody
	public List<Operator> chooseOperatorWindow(Model model,@RequestParam(defaultValue = "1") int page){
		Integer count = this.operatorService.queryAllOperatorCount();
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("pager", pager);
		List<Operator> list = this.operatorService.queryAllOperator(condition);

		return list;
	}
	
	
	/**
	 * 选择媒体场景
	 * 创建人：周俊
	 * 创建时间：2015.12.11 17：52
	 * 每个运营商场景显示：{场景ID，运营商名+运营商场景，数聚小场景}即：{id,getOperatorShowName(),placeId}其中placeId要传到后台
	 * @return
	 */
	@RequestMapping("chooseOperatorPlace")
	public String chooseOperatorPlaceWindow(Model model,@RequestParam(defaultValue = "1") int page){
		Integer count = this.placeOperatorService.queryOperatorPlaceCount();
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("pager", pager);
		List<PlaceOperator> list = this.placeOperatorService.queryOperatorPlace(condition);
		model.addAttribute("pager", pager);
		model.addAttribute("list", list);
		return page("operatorPlaceWindow");
	}
	
	/**
	 * 选择大场景
	 * 创建人：周俊
	 * 创建时间：2015-12-14 11.30
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("choosePlaceClass")
	public String choosePlaceClassWindow(Model model,@RequestParam(defaultValue = "1") int page){
		Integer count = this.placeClassInformationService.queryPlaceClassCount();
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("pager", pager);
		List<PlaceClassInformation> list = this.placeClassInformationService.queryPlaceClass(condition);
		model.addAttribute("pager", pager);
		model.addAttribute("list", list);
		return page("placeClassWindow");
	}
	
	/**
	 * 选择小场景
	 * @param model
	 * @param page
	 * @return
	 */
	@RequestMapping("choosePlace")
	public String choosePlaceWindow(Model model,@RequestParam(defaultValue = "1") int page){
		Integer count = this.placeService.queryPlaceCount();
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		Map<String,Object> condition = new HashMap<String,Object>();
		condition.put("pager", pager);
		List<Place> list = this.placeService.queryPlace(condition);
		model.addAttribute("pager", pager);
		model.addAttribute("list", list);
		return page("placeWindow");
	}
	
	/**
	 * 选择终端
	 * @param model
	 * @param page
	 * @param cnCity
	 * @param cn
	 * @return
	 */
	@RequestMapping("chooseWifi")
	public String chooseWifiWindow(Model model,@RequestParam(defaultValue = "1") int page,String cnCity,String cn){
		Map<String,Object> condition = new HashMap<String,Object>();
		if( null != cnCity && !"".equals(cnCity)){
			condition.put("cnCity", cnCity);
		}
		if( null != cn && !"".equals(cn)){
			condition.put("cn", cn);
		}
		
		Integer count = this.wifiService.queryWifiCount(condition);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(count);
		
		
		condition.put("pager", pager);
		List<Wifi> list = this.wifiService.queryWifi(condition);
		model.addAttribute("pager", pager);
		model.addAttribute("list", list);
		return page("wifiWindow");
	}
	
}
