package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.gamegroup.model.entity.DialogEntity;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ljy on 2018/3/29.
 */

public class DialogToastAct extends Activity {

    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.chec_cb)
    CheckBox checCb;
    @BindView(R.id.dialog_action)
    TextView dialogAction;
    @BindView(R.id.dialog_sure)
    TextView dialogSure;
    @BindView(R.id.dialog_close)
    ImageView dialogClose;
    @BindView(R.id.ems_game_layout)
    ConstraintLayout ems_game_layout;
    @BindView(R.id.game_account_et)
    EditText gameAccountEt;
    @BindView(R.id.game_pay_et)
    EditText gamePayEt;

    private DialogEntity dialog_entity;
    DownloadUtil mDownloadTaskService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_notice_layout);
        ButterKnife.bind(this);
        dialog_entity = (DialogEntity) getIntent().getSerializableExtra("dialog_entity");

        mDownloadTaskService = new DownloadUtil();
        initView();
    }

    private void initView() {
        if (dialog_entity == null) {
            return;
        }
        setView(titleTv, dialog_entity.getTitle());
        setView(contentTv, dialog_entity.getContext());
        setView(dialogAction, dialog_entity.getCancel());
        setView(dialogSure, dialog_entity.getSure());
        if (dialog_entity.getType() != null && dialog_entity.getType().equals("my_game")) {
            ems_game_layout.setVisibility(View.VISIBLE);
        } else {
            ems_game_layout.setVisibility(View.GONE);
        }

    }

    @OnClick({R.id.dialog_action, R.id.dialog_sure, R.id.dialog_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_action:
                downLoadValue(1);
                finish();
                break;
            case R.id.dialog_sure:
                if (dialog_entity.getEventTypes() != null) {
                    EventBus.getDefault().post(new BigEvent().setEventTypes(dialog_entity.getEventTypes()));
                }
                downLoadValue(2);
                myGame();//我的游戏
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.dialog_close:
                downLoadValue(0);
                finish();
                break;
        }
    }

    private void setView(View view, String string) {
        if (!TextUtils.isEmpty(string)) {
            view.setVisibility(View.VISIBLE);
            if (view instanceof TextView) {
                ((TextView) view).setText(string);
            }

        } else {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * 是否是下载过来的
     */
    private void downLoadValue(int flag) {
        if (dialog_entity.getDownLoadInfo() == null) {
            return;
        }
        SpUtils.saveDownNotice(this, flag);
        if (flag != 0)
            mDownloadTaskService.start(dialog_entity.getDownLoadInfo());
    }

    /**
     * 我的游戏
     */
    private void myGame() {
        if (dialog_entity.getType() != null && dialog_entity.getType().equals("my_game")) {
            String gameAccountEtStr = gameAccountEt.getText().toString().trim() +"";
            String gamePayEtStr = gamePayEt.getText().toString().trim() +"";
            if(TextUtils.isEmpty(gameAccountEtStr)){
                G.showToast("游戏账号不能为空!");
                return;
            }
            if(TextUtils.isEmpty(gamePayEtStr)){
                G.showToast("充值金额不能为空!");
                return;
            }

        }
    }
}
