package com.sheep.gamegroup.view.activity;

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

import com.kfzs.duanduan.event.BigEvent;
import com.kfzs.duanduan.services.DownloadTaskService;
import com.sheep.gamegroup.model.entity.DialogEntity;
import com.sheep.jiuyan.samllsheep.R;
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;

    private DialogEntity dialog_entity;
    private Activity activity;
    DownloadTaskService mDownloadTaskService;

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

        mDownloadTaskService = new DownloadTaskService(activity);
        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());

    }

    @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(false);
                finish();
                break;
            case R.id.dialog_sure:
                if(dialog_entity.getEventTypes() != null){
                    EventBus.getDefault().post(new BigEvent().setEventTypes(dialog_entity.getEventTypes()));
                }
                downLoadValue(true);
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.dialog_close:
                downLoadValue(false);
                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(boolean flag){
        if(dialog_entity.getDownLoadInfo() == null){
            return;
        }
        SpUtils.saveDownNotice(activity, flag);
        mDownloadTaskService.start(dialog_entity.getDownLoadInfo());
    }
}
