package com.example.controller;

import com.example.bean.Doctor;
import com.example.bean.RespBean;
import com.example.bean.User;
import com.example.bean.Worker;
import com.example.service.DoctorService;
import com.example.service.UserService;
import com.example.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class LoginRegController {
    @Autowired
    UserService userService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    WorkerService workerService;

    @PostMapping("/login")
    public RespBean login(@RequestBody RespBean respBean)
    {
        int i = selectSameId(respBean.getStatus());
        if(i == 0)//表明账户不存在
            return new RespBean("error","未找到账户");
        else if(i == 1)//表明账户是一个用户
        {
            User user1 = userService.getUserByUserId(respBean.getStatus());
            if(user1.getPassword().equals(respBean.getMsg()))
            {
                if(user1.isEnabled() == true)
                    return new RespBean("user","登录成功",user1);
                else
                    return new RespBean("error","账户无资格");
            }
            else
                return new RespBean("error","密码错误");
        }
        else if(i == 2)
        {
            Doctor doctor = doctorService.getDoctorById(respBean.getStatus());
            if(doctor.getPassword().equals(respBean.getMsg()))
            {
                if(doctor.isEnabled() == true)
                {
                    return new RespBean("doctor","登录成功",doctor);
                }
                else
                    return new RespBean("error","账户无资格");
            }
            else
                return new RespBean("error","密码错误");
        }
        else if(i == 3)
        {
            Worker worker = workerService.getWorkerById(respBean.getStatus());
            if(worker.getPassword().equals(respBean.getMsg()))
                if(worker.isEnabled() == true)
                    return new RespBean("worker","登录成功",worker);
                else
                    return new RespBean("error","账户无资格");
            else
                return new RespBean("error","密码错误");
        }

        return new RespBean("error","登录失败");
    }

    @PostMapping("/regUser")
    public RespBean regUser(@RequestBody User user)
    {
        System.out.println(user);
        int i = selectSameId(user.getUserid());
        if(i != 0)
            return new RespBean("error","有同名账户");
        else
        {
            int result = userService.reg(user);
            if(result == 0)
                return new RespBean("success","注册成功！");
            else
                return new RespBean("error","注册失败");
        }
    }

    @PostMapping("/regDoctor")
    public RespBean regDocotr(@RequestBody Doctor doctor) throws IOException {


        int i = selectSameId(doctor.getUserid());
        if(i != 0)
            return new RespBean("error","有同名账户");
        else
        {
            int result = doctorService.regDoctor(doctor);
            if(result == 0)
                return new RespBean("success","注册成功");
            else
                return new RespBean("error","注册失败");
        }
    }

    @PostMapping("/regWorker")
    public RespBean regWorker(@RequestBody Worker worker) throws IOException{

        int i = selectSameId(worker.getUserid());
        if(i != 0)
            return new RespBean("error","有同名账户");
        else
        {
            int result = workerService.regWorker(worker);
            if(result == 0)
                return new RespBean("success","注册成功");
            else
                return new RespBean("error","注册失败");
        }
    }

    @RequestMapping ("/uploadImg")
    public RespBean uploadImag(HttpServletRequest req, @RequestPart("file") MultipartFile image,@RequestParam("userid") String userid) throws IOException {
        //将文件转化为byte[]格式，并传给doctor
//        if (req instanceof MultipartHttpServletRequest) {
            byte[] bytes = image.getBytes();
            System.out.println("图片上传");
            int result = doctorService.updateQualification(bytes, userid);
            if(result == 1)
                return new RespBean("success","上传成功");
            else
                return new RespBean("error","上传失败");
            // process
//        }else {
//            return new RespBean("error","无法访问图片上传地址");
//        }
    }

    public int selectSameId(String userId)
    {
        User user = userService.getUserByUserId(userId);
        Doctor doctor = doctorService.getDoctorById(userId);
        Worker worker = workerService.getWorkerById(userId);

        if(user != null)//说明user中有相同的id
            return 1;
        else if(doctor != null)//说明doctor中有相同的id
            return 2;
        else if(worker != null)//说明worker中有相同的id
            return 3;
        else
            return 0;//说明没有相同的id
    }
}
