package com.iamberry.rent.handlers.factory;

import com.iamberry.rent.face.machine.MachineService;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.machine.Machine;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.rent.MachineInfo;
import com.iamberry.wechat.face.wechat.ConfigService;
import com.iamberry.wechat.iot.WechatIotUtils;
import com.iamberry.wechat.tools.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 工厂测试使用
 * @author 献
 * @company Goochine-深圳古辰创智科技有限公司
 * @website www.goochine.com
 * @tel 18271840547
 * @date 2017/4/26
 */
@RequestMapping("/wechat/factory")
@Controller
public class FactoryHandler {

    @Autowired
    private MachineService machineService;

    @Autowired
    private ConfigService configService;

    @ResponseBody
    @RequestMapping("/info_by_mac")
    public ResponseJson getDeviceInfoByMac(@RequestParam("mac") String mac/*, HttpServletRequest request*/) {
        // Member member = WechatUtils.getUserBySession(request);
        MachineInfo machine = new MachineInfo();
        machine.setMachineMac(mac);
        List<MachineInfo> list = machineService.list(machine);
        if (list == null || list.isEmpty()) {
            // 资源为空
            return new ResponseJson(200, "SUCCESS", 404);
        }
        // 返回机器信息
        return new ResponseJson(200, "SUCCESS", 200).addResponseKeyValue("info", list.get(0));
    }


    /**
     * 取消绑定
     * @return
     */
    @ResponseBody
    @RequestMapping("/un_bind")
    public ResponseJson unBind(@RequestParam("ticket") String ticket,
                               @RequestParam("device_id") String device_id,
                               HttpServletRequest request) {
        Member member = WechatUtils.getUserBySession(request);

        // 取消用户和机器绑定
        net.sf.json.JSONObject json = WechatIotUtils.unBind(device_id, ticket, member.getUserOpenid(), configService.getAccessToken(), true);
        if (json.getJSONObject("base_resp").getInt("errcode") != 0) {
            return new ResponseJson(200, "ERROR", 500);
        }
        return new ResponseJson(200, "SUCCESS", 200);
    }

    /**
     * 绑定
     * @return
     */
    @ResponseBody
    @RequestMapping("/bind")
    public ResponseJson bind(@RequestParam("ticket") String ticket,
                             @RequestParam("device_id") String device_id,
                             HttpServletRequest request) {
        Member member = WechatUtils.getUserBySession(request);

        // 取消用户和机器绑定
        net.sf.json.JSONObject json = WechatIotUtils.unBind(device_id, ticket, member.getUserOpenid(), configService.getAccessToken(), false);
        if (json.getJSONObject("base_resp").getInt("errcode") != 0) {
            return new ResponseJson(200, "ERROR", 500);
        }
        return new ResponseJson(200, "SUCCESS", 200);
    }

}
