package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dao.VuforiaImageGroupMapper;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.VuforiaImageGroup;
import com.izouma.awesomeadmin.model.VuforiaTarget;
import com.izouma.awesomeadmin.vuforia.VuforiaUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.VuforiaImageInfo;
import com.izouma.awesomeadmin.service.VuforiaImageInfoService;
import com.izouma.awesomeadmin.dao.VuforiaImageInfoMapper;

/**
 * service接口实现类
 */
@Service
public class VuforiaImageInfoServiceImpl implements VuforiaImageInfoService {

    private static Logger logger = Logger.getLogger(VuforiaImageInfoServiceImpl.class);

    @Autowired
    private VuforiaImageInfoMapper vuforiaImageInfoMapper;
    @Autowired
    private VuforiaImageGroupMapper vuforiaImageGroupMapper;

    @Override
    public List<VuforiaImageInfo> getVuforiaImageInfoList(VuforiaImageInfo record) {

        logger.info("getVuforiaImageInfoList");
        try {

            return vuforiaImageInfoMapper.queryAllVuforiaImageInfo(record);
        } catch (Exception e) {
            logger.error("getVuforiaImageInfoList", e);
        }

        return null;
    }

    @Override
    public List<VuforiaImageInfo> getVuforiaImageInfoByPage(Page page, VuforiaImageInfo record) {

        logger.info("getVuforiaImageInfoByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return vuforiaImageInfoMapper.queryVuforiaImageInfoByPage(parameter);
        } catch (Exception e) {
            logger.error("getVuforiaImageInfoByPage", e);
        }

        return null;
    }

    @Override
    public VuforiaImageInfo getVuforiaImageInfoById(String id) {

        logger.info("getVuforiaImageInfoyId");
        try {

            return vuforiaImageInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getVuforiaImageInfoById", e);
        }

        return null;
    }

    @Override
    public VuforiaTarget getVuforiaTarget(String id) {

        logger.info("getVuforiaTarget");
        try {

            VuforiaImageInfo vuforiaImageInfo = vuforiaImageInfoMapper.selectByPrimaryKey(Integer.valueOf(id));

            if (StringUtils.isNotEmpty(vuforiaImageInfo.getVuforiaImageGroupId())) {
                VuforiaImageGroup vuforiaImageGroup = vuforiaImageGroupMapper.selectByPrimaryKey(Integer.valueOf(vuforiaImageInfo.getVuforiaImageGroupId()));

                if (vuforiaImageGroup != null) {

                    String accessKey = vuforiaImageGroup.getAccessKey();
                    String secretKey = vuforiaImageGroup.getSecretKey();
                    String targetId = vuforiaImageInfo.getTargetId();

                    if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey) && StringUtils.isNotEmpty(targetId)) {
                        return VuforiaUtil.getTarget(targetId, accessKey, secretKey);
                    }


                }
            }

        } catch (Exception e) {
            logger.error("getVuforiaTarget", e);
        }

        return null;
    }

    @Override
    public VuforiaImageInfo getVuforiaImageInfo(VuforiaImageInfo record) {

        logger.info("getVuforiaImageInfo");
        try {

            return vuforiaImageInfoMapper.queryVuforiaImageInfo(record);
        } catch (Exception e) {
            logger.error("getVuforiaImageInfo", e);
        }

        return null;
    }

    @Override
    public boolean createVuforiaImageInfo(VuforiaImageInfo record) {

        logger.info("createVuforiaImageInfo");
        try {

            if (StringUtils.isNotEmpty(record.getVuforiaImageGroupId())) {//保存识别图

                VuforiaImageGroup vuforiaImageGroup = vuforiaImageGroupMapper.selectByPrimaryKey(Integer.valueOf(record.getVuforiaImageGroupId()));

                if (vuforiaImageGroup != null) {

                    String accessKey = vuforiaImageGroup.getAccessKey();
                    String secretKey = vuforiaImageGroup.getSecretKey();
                    String targetName = record.getName();
                    double width = 20.0;
                    String imageUrl = record.getVuforiaImage();

                    if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey)) {

                        String targetId = VuforiaUtil.PostNewTarget(imageUrl, targetName, width, accessKey, secretKey);
                        if (StringUtils.isNotEmpty(targetId)) {
                            record.setTargetId(targetId);
                        }

                    }

                }

            }

            int updates = vuforiaImageInfoMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createVuforiaImageInfo", e);
        }

        return false;
    }

    @Override
    public Result deleteVuforiaImageInfo(VuforiaImageInfo record) {

        logger.info("deleteVuforiaImageInfo");
        try {

            VuforiaImageInfo vuforiaImageInfo = vuforiaImageInfoMapper.selectByPrimaryKey(record.getId());

            if (StringUtils.isNotEmpty(vuforiaImageInfo.getVuforiaImageGroupId())) {
                VuforiaImageGroup vuforiaImageGroup = vuforiaImageGroupMapper.selectByPrimaryKey(Integer.valueOf(vuforiaImageInfo.getVuforiaImageGroupId()));

                if (vuforiaImageGroup != null) {

                    String accessKey = vuforiaImageGroup.getAccessKey();
                    String secretKey = vuforiaImageGroup.getSecretKey();
                    String targetId = vuforiaImageInfo.getTargetId();

                    if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey) && StringUtils.isNotEmpty(targetId)) {
                        String result_code = VuforiaUtil.deleteTarget(targetId, accessKey, secretKey);
                        if (!"Success".equals(result_code) && !"UnknownTarget".equals(result_code)) {
                            return new Result(false, "删除失败：" + result_code + "，请稍后再试！");
                        }

                    }


                }
            }


            int updates = vuforiaImageInfoMapper.delete(record);

            if (updates > 0) {


                return new Result(true, "删除成功");
            }
        } catch (Exception e) {
            logger.error("deleteVuforiaImageInfo", e);
        }

        return new Result(false, "删除异常");
    }

    @Override
    public boolean updateVuforiaImageInfo(VuforiaImageInfo record) {

        logger.info("updateVuforiaImageInfo");
        try {
            VuforiaImageInfo vuforiaImageInfo = vuforiaImageInfoMapper.selectByPrimaryKey(record.getId());

            int updates = vuforiaImageInfoMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {

                if (StringUtils.isNotEmpty(record.getVuforiaImageGroupId())) {//保存识别图

                    VuforiaImageGroup vuforiaImageGroup = vuforiaImageGroupMapper.selectByPrimaryKey(Integer.valueOf(record.getVuforiaImageGroupId()));

                    if (vuforiaImageGroup != null) {

                        String accessKey = vuforiaImageGroup.getAccessKey();
                        String secretKey = vuforiaImageGroup.getSecretKey();
                        String targetId = vuforiaImageInfo.getTargetId();

                        String targetName = null;
                        if (StringUtils.isNotEmpty(record.getName())) {
                            if (!vuforiaImageInfo.getName().equals(record.getName())) {
                                targetName = record.getName();
                            }
                        }

                        String imageUrl = null;

                        if (StringUtils.isNotEmpty(record.getVuforiaImage())) {
                            if (!vuforiaImageInfo.getVuforiaImage().equals(record.getVuforiaImage())) {
                                imageUrl = record.getVuforiaImage();
                            }

                        }

                        if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey) && StringUtils.isNotEmpty(targetId)) {

                            if (StringUtils.isNotEmpty(targetName) || StringUtils.isNotEmpty(imageUrl)) {
                                VuforiaUtil.updateTarget(targetId, targetName, 0, imageUrl, accessKey, secretKey, null);
                            }


                        }

                    }

                }

                return true;
            }
        } catch (Exception e) {
            logger.error("updateVuforiaImageInfo", e);
        }

        return false;
    }
}

