package com.lightinit.hsdatashow.impl;

import com.lightinit.hsdatashow.common.CharEncoding;
import com.lightinit.hsdatashow.common.HttpTookitUtils;
import com.lightinit.hsdatashow.common.MD5Utils;
import com.lightinit.hsdatashow.json.pojo.oauth.AccessTokenEntity;
import com.lightinit.hsdatashow.json.pojo.oauth.OAuthTokenEntity;
import com.lightinit.hsdatashow.service.IOAuthService;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/12/11.
 */
@Service
public class OAuthServiceImpl implements IOAuthService {
    @Value("${oauthserver.client.auth_url}")
    private String accesstoken_oauth_url;
    @Value("${oauthserver.client.token_url}")
    private String accesstoken_token_url;
    @Value("${oauthserver.client.response_type}")
    private String response_type;
    @Value("${oauthserver.client.id}")
    private String clientId;
    @Value("${oauthserver.client.secret}")
    private String clientSecret;
    @Value("${oauthserver.client.scope}")
    private String scope;
    @Value("${oauthserver.client.redirect_url}")
    private String redirect_url;
    @Value("${oauthserver.client.grant_type}")
    private String grant_type;
    @Value("${oauthserver.client.serial}")
    private String clientSerial;

    @Override
    public String accessToken() {
        String accessToken="";
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("client_id",clientId));
        nameValuePairs.add(new BasicNameValuePair("client_secret",clientSecret));
        nameValuePairs.add(new BasicNameValuePair("client_sn", MD5Utils.MD5(clientSerial)));
        nameValuePairs.add(new BasicNameValuePair("response_type",response_type));
        nameValuePairs.add(new BasicNameValuePair("grant_type",grant_type));
        nameValuePairs.add(new BasicNameValuePair("redirect_uri",redirect_url));
        nameValuePairs.add(new BasicNameValuePair("scope",scope));
        String responseBody= HttpTookitUtils.doGet(accesstoken_oauth_url,nameValuePairs, CharEncoding.UTF8);
        if(StringUtils.isEmpty(responseBody)==false){
            try{
                ObjectMapper objectMapper=new ObjectMapper();
                AccessTokenEntity mAccessTokenEntity= objectMapper.readValue(responseBody,AccessTokenEntity.class);
                if(mAccessTokenEntity!=null){
                    accessToken=mAccessTokenEntity.getAccessToken();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return accessToken;
    }

    @Override
    public OAuthTokenEntity accessToken(String code) {
        OAuthTokenEntity accessToken=null;
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("client_id",clientId));
        nameValuePairs.add(new BasicNameValuePair("client_secret",clientSecret));
        nameValuePairs.add(new BasicNameValuePair("response_type",response_type));
        nameValuePairs.add(new BasicNameValuePair("grant_type",grant_type));
        nameValuePairs.add(new BasicNameValuePair("redirect_uri",redirect_url));
        nameValuePairs.add(new BasicNameValuePair("code",code));
        String responseBody= HttpTookitUtils.doPost(accesstoken_token_url,nameValuePairs, CharEncoding.UTF8);
        if(StringUtils.isEmpty(responseBody)==false){
            try{
                ObjectMapper objectMapper=new ObjectMapper();
                accessToken= objectMapper.readValue(responseBody,OAuthTokenEntity.class);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return accessToken;
    }
}
