package com.android.chmo.ui.activity.promote;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.GetCurrentShareCoinResponse;
import com.android.chmo.http.response.TransferCoinResponse;
import com.android.chmo.http.service.UserService;
import com.android.chmo.im.IMManager;
import com.android.chmo.ui.activity.LoginActivity;
import com.android.chmo.ui.activity.WithdrawActivity;
import com.android.chmo.ui.dialog.ConfirmDialog;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.LoginUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class PromoteActivity extends BaseActivity {
    @BindView(R.id.tv_coin)
    TextView tvCoin;

    private double total     = 0;
    private double available = 0;

    @Override
    protected int getStatusBarStyle() {
        return STATUS_BAR_STYLE_LIGHT_CONTENT;
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_promote;
    }

    @Override
    public void initView() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @OnClick({R.id.back, R.id.tv_detail, R.id.tv_withdraw, R.id.tv_transfer, R.id.btn_invite})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_detail:
                openPage(PromoteDetailActivity.class);
                break;
            case R.id.tv_withdraw:
                WithdrawActivity.open(this, available);
                overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
                break;
            case R.id.tv_transfer:
                new ConfirmDialog(this)
                        .setMsg("确认转到M币？")
                        .setDialogListener(new ConfirmDialog.DialogListener() {
                            @Override
                            public void onOk(Dialog dialog) {
                                dialog.dismiss();
                                transfer();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        }).show();
                break;
            case R.id.btn_invite:
                openPage(ShareActivity.class);
                break;
        }
    }

    void getData() {
        UserService.getCurrentShareCoin(new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                GetCurrentShareCoinResponse response = new Gson().fromJson(result, GetCurrentShareCoinResponse.class);
                if (response.isSuccess()) {
                    total = response.getTotal();
                    available = response.getAvailable();
                    tvCoin.setText(String.valueOf(total));
                }
            }

            @Override
            public void onFailure(String error) {

            }
        });
    }

    void transfer() {
        showLoading();
        UserService.transferCoin(new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                TransferCoinResponse response = new Gson().fromJson(result, TransferCoinResponse.class);
                if (response.isSuccess()) {
                    showToast("转出成功");
                } else {
                    showToast(response.getError());
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
            }
        });
    }
}
