package org.usc.check.in.task;

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

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.usc.check.in.model.Account;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 *
 * @author Shunli
 */
@Component
public class ZiMuZuTvSignInTask extends BaseTask {
    private static final Logger log = LoggerFactory.getLogger(ZiMuZuTvSignInTask.class);

    private static final String URL = "http://www.zimuzu.tv/";
    private static final String LOGIN_URL = "http://www.zimuzu.tv/User/Login/ajaxLogin";
    private static final String SIGN_IN_PAGE_URL = "http://www.zimuzu.tv/user/sign";
    private static final String SIGN_IN_URL = "http://www.zimuzu.tv/user/sign/dosign";

    @Override
    protected String name() {
        return "zimuzu";
    }

    @Scheduled(cron = "0 0 7,18 * * ?")
    public void run() {
        for (Account account : buildAccounts()) {
            try {
                Executor executor = Executor.newInstance().use(new BasicCookieStore());
                if (login(executor, account)) {
                    signIn(executor, account);
                }
            } catch (Exception e) {
                log.error("【ZIMUZU】【" + account.getUsername() + "】签到异常", e);
            }
        }
    }

    private boolean login(Executor executor, Account account) throws ClientProtocolException, IOException {
        String usrename = account.getUsername();

        List<NameValuePair> formParams = new ArrayList<NameValuePair>();
        formParams.add(new BasicNameValuePair("account", usrename));
        formParams.add(new BasicNameValuePair("password", account.getPassword()));
        formParams.add(new BasicNameValuePair("remember", "0"));
        formParams.add(new BasicNameValuePair("url_back", URL));

        String loginJson = executor.execute(appendTimeOuts(Request.Post(LOGIN_URL)).bodyForm(formParams)).returnContent().asString();
        JSONObject loginJsonParseObject = JSON.parseObject(loginJson);
        if (1 != loginJsonParseObject.getInteger("status")) {
            log.info("【ZIMUZU】【{}】登录失败：{}", usrename, loginJsonParseObject.getString("info"));
            return false;
        }

        log.info("【ZIMUZU】【{}】登录成功", usrename);
        return true;
    }

    private boolean signIn(Executor executor, Account account) throws ClientProtocolException, IOException, InterruptedException {
        String usrename = account.getUsername();

        // first load signin page
        executor.execute(appendTimeOuts(Request.Get(SIGN_IN_PAGE_URL))).discardContent();

        // sleep 15s+
        TimeUnit.SECONDS.sleep(20);

        String signInJson = executor.execute(appendTimeOuts(Request.Get(SIGN_IN_URL))).returnContent().asString();
        JSONObject signInParseObject = JSON.parseObject(signInJson);
        Integer stauts = signInParseObject.getInteger("status");
        if (stauts != null) {
            if (0 == stauts) {
                log.info("【ZIMUZU】【{}】已经签到", usrename);
                return true;
            }

            if (1 == stauts) {
                log.info("【ZIMUZU】【{}】签到成功", usrename);
                return true;
            }
        }

        log.info("【ZIMUZU】【{}】签到失败：{}", usrename, signInParseObject.getString("info"));
        return false;
    }

}

@@ -113,47 +113,54 @@
 t;%0D%0A
+%0D%0A
 import 
-java.util.concurrent.TimeUnit;%0D%0A
+org.apache.commons.lang3.StringUtils;
 %0D%0Aim
@@ -1126,21 +1126,16 @@
 SIGN_IN_
-PAGE_
 URL = %22h
@@ -1170,96 +1170,8 @@
 n%22;%0D
-%0A    private static final String SIGN_IN_URL = %22http://www.zimuzu.tv/user/sign/dosign%22;%0D
 %0A%0D%0A 
@@ -2938,43 +2938,8 @@
 %0D%0A%0D%0A
-        // first load signin page%0D%0A
     
@@ -2990,21 +2990,8 @@
 Get(
-SIGN_IN_PAGE_
 URL)
@@ -3026,86 +3026,17 @@
     
-// sleep 15s+%0D%0A        TimeUnit.SECONDS.sleep(20);%0D%0A%0D%0A        String signInJso
+String rt
 n = 
@@ -3135,330 +3135,47 @@
     
-JSONObject signInParseObject = JSON.parseObject(signInJson);%0D%0A        Integer stauts = signInParseObject.getInteger(%22status%22);%0D%0A        if (stauts != null) %7B%0D%0A            if (0 == stauts) %7B%0D%0A                log.info(%22%E3%80%90ZIMUZU%E3%80%91%E3%80%90%7B%7D%E3%80%91%E5%B7%B2%E7%BB%8F%E7%AD%BE%E5%88%B0%22, usrename);%0D%0A                return true;%0D%0A            %7D%0D%0A%0D%0A            if (1 == stauts
+if (StringUtils.contains(rtn, %22%E6%82%A8%E4%BB%8A%E5%A4%A9%E5%B7%B2%E7%99%BB%E5%BD%95%22)
 ) %7B%0D
@@ -3179,36 +3179,32 @@
  %7B%0D%0A            
- 
-   
 log.info(%22%E3%80%90ZIMUZ
@@ -3236,28 +3236,24 @@
             
-    
 return true;
@@ -3254,31 +3254,16 @@
  true;%0D%0A
-            %7D%0D%0A
         
@@ -3301,19 +3301,16 @@
 %E3%80%90%7B%7D%E3%80%91%E7%AD%BE%E5%88%B0%E5%A4%B1%E8%B4%A5
-%EF%BC%9A%7B%7D
 %22, usren
@@ -3316,45 +3316,8 @@
 name
-, signInParseObject.getString(%22info%22)
 );%0D%0A

update zimuzutv signin logic.