/**
 * Copyright (c) 2013 DuoKu Inc.
 */

package com.caimai.platform.demo.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.caimai.platform.demo.R;
import com.duoku.platform.DkErrorCode;
import com.duoku.platform.DkPlatform;
import com.duoku.platform.DkProtocolConfig;
import com.duoku.platform.DkProtocolKeys;
import com.duoku.platform.IDKSDKCallBack;
import com.duoku.platform.ui.DKContainerActivity;

public class UserLoginActivity extends BaseActivity implements OnClickListener {
    private View mAccountLoginView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.userlogin);

        ((TextView) findViewById(R.id.tv_versioncode))
                .setText(getVersionName());
        findViewById(R.id.btn_layout).setVisibility(View.VISIBLE);
        mAccountLoginView = findViewById(R.id.btn_account_login);
        mAccountLoginView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mAccountLoginView) {
            duokuLogin();
        }
    }

    private void duokuLogin() {
        DkPlatform.invokeActivity(this, getLoginIntent(), new IDKSDKCallBack() {
            @Override
            public void onResponse(String paramString) {
                // onResponse()方法中的paramString为返回结果JSON串，例如：登录成功通知内容格式如下：
                // {"user_id":"103256","user_name":"dktest","user_sessionid":"52469875215469875462545698754562","state_code":1021}
                // 其中：state_code为状态码，"user_id"为用户id,
                // "user_name"为用户的用户名，"user_sessionid"为用户的sessionid
                int _loginState = 0;
                String _userName = null;
                String _userId;
                String _userSessionId;
                JSONObject jsonObj;
                try {
                    jsonObj = new JSONObject(paramString);
                    _loginState = jsonObj
                            .getInt(DkProtocolKeys.FUNCTION_STATE_CODE);// 用户登录状态
                    _userName = jsonObj.getString(DkProtocolKeys.USER_NAME);// 用户姓名
                    _userId = jsonObj.getString(DkProtocolKeys.USER_ID);// 用户id
                    _userSessionId = jsonObj
                            .getString(DkProtocolKeys.USER_SESSIONID);// 用户sessionId
                    MainActivity.userid = _userId;
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (DkErrorCode.DK_LOGIN_SUCCESS == _loginState) {
                    // 登录成功回调，必须正确处理
                    Toast.makeText(UserLoginActivity.this, "登录成功",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UserLoginActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                } else if (DkErrorCode.DK_LOGIN_CANCELED == _loginState) {
                    // 取消登录回调，必须正确处理
                    Toast.makeText(UserLoginActivity.this, "用户取消登录",
                            Toast.LENGTH_SHORT).show();
                } else if (DkErrorCode.DK_NEEDLOGIN == _loginState) {
                    // 用户登录状态失效回调，必须正确处理
                    DkPlatform.destroy(UserLoginActivity.this);
                    Intent i = new Intent(UserLoginActivity.this,
                            UserLoginActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    UserLoginActivity.this.startActivity(i);
                }
            }
        });
    }

    public Intent getLoginIntent() {
        Bundle bundle = new Bundle();
        bundle.putInt(DkProtocolKeys.FUNCTION_CODE,
                DkProtocolConfig.FUNCTION_LOGIN);
        Intent intent = new Intent(this, DKContainerActivity.class);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    private String getVersionName() {
        // 获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo;
        try {
            packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return packInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";

    }

}
