package edu.neusoft.project.controller;

import edu.neusoft.project.model.service.IndentService;
import edu.neusoft.project.model.service.PersonService;
import edu.neusoft.project.model.service.SongService;
import edu.neusoft.project.model.vo.Person;
import edu.neusoft.project.model.vo.Song;
import edu.neusoft.project.util.Thread.GetMusicFromNeteaseThreadAndIns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
//@RequestMapping(path = "/song")
public class ControlBasicAction {
    @Autowired
    private PersonService service;
    @Autowired
    private SongService songService;
    @Autowired
    private IndentService indentService;

    @ResponseBody
    @RequestMapping("addFromNetease.action")
    public int addSong(String id){
        int ret=0;


        try {
            int intid=Integer.parseInt(id);
            GetMusicFromNeteaseThreadAndIns ins=new GetMusicFromNeteaseThreadAndIns(intid);
            ret = ins.call();
            System.out.println(ret);
        } catch (NumberFormatException e) { //NumberFormatException ;
            ret = 5;
        }catch (Exception e){
            ret=0;
        }


        System.out.println("FromNetease:"+id);
        return ret;
    }


    @RequestMapping("/song/{idStr}.m")
    public String shared(@PathVariable String idStr, ModelMap model,HttpServletRequest request){
        System.out.println("url访问:id--"+idStr);
        HttpSession sessions = request.getSession();
        Person loginedPer= new Person();
        loginedPer= (Person) sessions.getAttribute("loginedPer");
//        System.out.println(searchText+"0000000000000000");
        if (idStr.equals("")||idStr==null){
            if (loginedPer!=null){
                String level = service.getLevel(loginedPer);
                List<Song> list04 = songService.likeSongs();
                model.put("list",list04);
                model.put("username",loginedPer.getUsername());
                model.put("password",loginedPer.getPassword());
                model.put("personCode",loginedPer.getPersonCode());
                model.put("personLevel",level);
                model.put("personListened",loginedPer.getPersonListened());
                System.out.println();

                return "logined";
            }
            return "Homepage";
        }
        Song song;
        song = songService.selectByIdPrecise(idStr);
//        if (song==null){
//            return "error";
//        }
        System.out.println("idStr:"+idStr);
        System.out.println(song);
        //likes
        List<Song> list = songService.likeSongs();
        //putModel
        if (song!=null){
            model.put("searchedSong", song);
        }else {
            model.put("searchedSong", "");
            model.put("searchedSongid","");
        }
        model.put("list",list);
        if(loginedPer!=null){
            String level = service.getLevel(loginedPer);
            songService.addListened(loginedPer);
            model.put("username",loginedPer.getUsername());
            model.put("password",loginedPer.getPassword());
            model.put("personCode",loginedPer.getPersonCode());
            model.put("personLevel",level);
            model.put("personListened",loginedPer.getPersonListened());
            System.out.println("loginedPer---------"+loginedPer);
            if (song==null){
                return "error";
            }
            return "logined";
        }
        if (song==null){
            return "ErrorUnLogined";
        }
        return "searchedButNotLogined";
    }



}
