package com.yutou.tools.Tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.ToolsPasswordDao;
import com.yutou.tools.mybatis.dao.ToolsPasswordTypeDao;
import com.yutou.tools.mybatis.model.ToolsPassword;
import com.yutou.tools.mybatis.model.ToolsPasswordExample;
import com.yutou.tools.mybatis.model.ToolsPasswordType;
import com.yutou.tools.mybatis.model.ToolsPasswordTypeExample;
import com.yutou.tools.other.tools;
import com.yutou.tools.utils.AESTools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
public class PasswordManager {

    @Resource
    ToolsPasswordDao passwordDao;
    @Resource
    ToolsPasswordTypeDao passwordTypeDao;
    @Resource
    tools tls;

    @ResponseBody
    @RequestMapping(value = "/tools/password/get/list.do",method = RequestMethod.GET)
    public String getPasswordList(HttpServletRequest request,String type){
        JSONObject json=new JSONObject();
        int id=tls.getUid(request);
        ToolsPasswordExample example=new ToolsPasswordExample();
        example.createCriteria().andTypeEqualTo(Integer.parseInt(type)).andUidEqualTo(id);
        List<ToolsPassword> passwords=passwordDao.selectByExample(example);
        for (ToolsPassword password : passwords) {
            password.setPassword("*****");
        }
        json.put("code",0);
        json.put("msg","ok");
        json.put("data", JSONArray.toJSON(passwords));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/get/password.do",method = RequestMethod.GET)
    public String getPassword(HttpServletRequest request,String id){
        JSONObject json=new JSONObject();
        int uid=tls.getUid(request);
        ToolsPasswordExample example=new ToolsPasswordExample();
        example.createCriteria().andIdEqualTo(Integer.parseInt(id)).andUidEqualTo(uid);
        ToolsPassword password=passwordDao.selectByExample(example).get(0);
        json.put("code",0);
        json.put("data",AESTools.decrypt(password.getPassword()));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/get/all.do",method = RequestMethod.GET)
    public String getAllPassword(HttpServletRequest request,String search){
        JSONObject json=new JSONObject();
        JSONArray array=new JSONArray();
        int uid=tls.getUid(request);
        if(uid==-1){
            return "";
        }
        ToolsPasswordExample example=new ToolsPasswordExample();
        ToolsPasswordExample.Criteria criteria=example.createCriteria();
        criteria.andUidEqualTo(uid);
        if(!StringUtils.isEmpty(search)){
            criteria.search(search);
        }
        List<ToolsPassword> list=passwordDao.selectByExample(example);
        for (ToolsPassword password : list) {
            JSONObject item=new JSONObject();
            item.put("id",password.getId());
            item.put("account",password.getUsername());
            item.put("title",password.getTitle());
            item.put("desc",password.getInfo());
            item.put("url",password.getUrl());
            item.put("password",AESTools.decrypt(password.getPassword()));
            array.add(item);
        }
        json.put("code",0);
        json.put("data",array);
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/type/get/list.do",method = RequestMethod.GET)
    public String getPasswordType(HttpServletRequest request){
        JSONObject json=new JSONObject();
        int uid=tls.getUid(request);
        ToolsPasswordTypeExample example=new ToolsPasswordTypeExample();
        example.createCriteria().andUidEqualTo(uid);
        List<ToolsPasswordType> toolsPasswordTypes=passwordTypeDao.selectByExample(example);
        json.put("code",0);
        json.put("msg","ok");
        json.put("data", JSONArray.toJSON(toolsPasswordTypes));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/type/set/add.do",method = RequestMethod.POST)
    public String addPasswordType(HttpServletRequest request,String type){
        JSONObject json=new JSONObject();
        try{
            int uid=tls.getUid(request);
            ToolsPasswordType passwordType=new ToolsPasswordType();
            passwordType.setTitle(type);
            passwordType.setUid(uid);
            int code=passwordTypeDao.insert(passwordType);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/set/add.do",method = RequestMethod.POST)
    public String addPassword(HttpServletRequest request,String title,String username,String password,String url,String info,String type){
        JSONObject json=new JSONObject();
        try{
            if(StringUtils.isEmpty(title)||StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(type)){
                System.out.println("tt "+request.getParameter("title"));
                System.out.println("title = " + title + ", username = " + username + ", password = " + password + ", url = " + url + ", info = " + info + ", type = " + type);
                json.put("code",-1);
                json.put("msg","有参数为空");
                return json.toJSONString();
            }
            int uid=tls.getUid(request);
            if(uid==-1){
                json.put("code",-1);
                json.put("msg","有参数为空");
                return json.toJSONString();
            }
            if("-1".equals(type)){
                int _type=-1;
                ToolsPasswordTypeExample typeExample=new ToolsPasswordTypeExample();
                typeExample.createCriteria().andTitleEqualTo("默认");
                if(passwordTypeDao.selectByExample(typeExample).isEmpty()){
                    ToolsPasswordType passwordType=new ToolsPasswordType();
                    passwordType.setTitle("默认");
                    passwordType.setUid(uid);
                    passwordTypeDao.insert(passwordType);
                    _type=passwordTypeDao.selectByExample(typeExample).get(0).getId();
                }else{
                    List<ToolsPasswordType> _type_list= passwordTypeDao.selectByExample(typeExample);
                    _type=_type_list.get(_type_list.size()-1).getId();
                }
                type=_type+"";
            }
            ToolsPassword toolsPassword=new ToolsPassword();
            toolsPassword.setTitle(title);
            toolsPassword.setUsername(username);
            toolsPassword.setPassword(AESTools.encrypt(password));
            toolsPassword.setType(Integer.parseInt(type));
            toolsPassword.setUid(uid);
            if(url!=null){
                toolsPassword.setUrl(url);
            }
            if(info!=null){
                toolsPassword.setInfo(info);
            }
            int code=passwordDao.insert(toolsPassword);
            json.put("code",0);
            json.put("msg","添加结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/set/update.do",method = RequestMethod.POST)
    public String updatePassword(HttpServletRequest request,String title,String username,String password,String url,String info,String id){
        JSONObject json=new JSONObject();
        try{
            int uid=tls.getUid(request);
            ToolsPasswordExample example=new ToolsPasswordExample();
            example.createCriteria().andIdEqualTo(Integer.parseInt(id)).andUidEqualTo(uid);

            ToolsPassword toolsPassword=passwordDao.selectByExample(example).get(0);
            if(!StringUtils.isEmpty(title)){
                toolsPassword.setTitle(title);
            }
            if(!StringUtils.isEmpty(username)){
                toolsPassword.setUsername(username);
            }
            if(!StringUtils.isEmpty(password)){
                toolsPassword.setPassword(AESTools.encrypt(password));
            }
            if(!StringUtils.isEmpty(url)){
                toolsPassword.setUrl(url);
            }
            if(!StringUtils.isEmpty(info)){
                toolsPassword.setInfo(info);
            }
            int code=passwordDao.updateByPrimaryKey(toolsPassword);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/type/set/update.do",method = RequestMethod.POST)
    public String updatePasswordType(HttpServletRequest request,String type,String id){
        JSONObject json=new JSONObject();
        try {
            int uid=tls.getUid(request);
            ToolsPasswordTypeExample example=new ToolsPasswordTypeExample();
            example.createCriteria().andUidEqualTo(uid).andIdEqualTo(Integer.parseInt(id));
            ToolsPasswordType passwordType=passwordTypeDao.selectByExample(example).get(0);
            if(!StringUtils.isEmpty(type)){
                passwordType.setTitle(type);
            }
            int code=passwordTypeDao.updateByPrimaryKey(passwordType);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/set/remove.do",method = RequestMethod.POST)
    public String removePassword(HttpServletRequest request,String id){
        JSONObject json=new JSONObject();
        try {
            int uid=tls.getUid(request);
            ToolsPasswordExample example=new ToolsPasswordExample();
            example.createCriteria().andUidEqualTo(uid).andIdEqualTo(Integer.parseInt(id));
            int code=passwordDao.deleteByExample(example);
            json.put("code",0);
            json.put("msg","删除结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "/tools/password/type/set/remove.do",method = RequestMethod.POST)
    public String removePasswordType(HttpServletRequest request,String id){
        JSONObject json=new JSONObject();
        try {
            int uid=tls.getUid(request);
            ToolsPasswordExample example=new ToolsPasswordExample();
            example.createCriteria().andTypeEqualTo(Integer.parseInt(id)).andUidEqualTo(uid);
            int passwordCode=passwordDao.deleteByExample(example);
            System.out.println("删除结果："+passwordCode);
            int code=passwordTypeDao.deleteByPrimaryKey(Integer.parseInt(id));
            json.put("code",0);
            json.put("msg","删除结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
}
