package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataKey;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_CLEAN;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_CLEAN_SURE;

/**
 * Created by realicing on 2018/04/.
 * realicing@sina.com
 */
public class ActSetting extends BaseActivity {

    private Activity activity;

    @Override
    protected int getLayoutId() {
        return R.layout.act_setting;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "设置")
                .setTitleFinish(this);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        initCacheSize();
    }

    @OnClick({R.id.change_password_layout, R.id.abourt_us_layout, R.id.clear_layout, R.id.tv_submit, R.id.change_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_password_layout:
                Jump2View.getInstance().goChangePassword(activity);
                break;
            case R.id.abourt_us_layout://关于我们
                Jump2View.getInstance().goNewAboutUs(activity);
                break;
            case R.id.change_layout://新功能介绍
                Jump2View.getInstance().goVersionChange(activity, ApkUtils.getCurrentPkgVersionName(SheepApp.getInstance()));
                break;
            case R.id.clear_layout://清除缓存
                ViewUtil.showMsgDialog(activity, new DialogConfig().setTitle("清除缓存").setMsg("所有本地的缓存都会被清理").setBtnRightText("取消")
                        .setBtnLeftText("确定").setBtnLeftOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                USER_CLEAN_SURE.onEvent();
                                SysAppUtil.clearCache(new AbsObserver<Object>() {
                                    @Override
                                    public void onNext(Object o) {
                                        DataUtil.putAsInt(DataKey.KEY_ORDER, 1);
                                        G.showToast("清理缓存完成");
                                        initCacheSize();
                                    }
                                });
                            }
                        }));
                USER_CLEAN.onEvent();
                break;
            case R.id.tv_submit:
                CommonUtil.getInstance().loginOut((BaseActivity) activity);
                break;
        }
    }

    @BindView(R.id.clear_item_tv)
    TextView clear_item_tv;

    private void initCacheSize() {
        String size = SysAppUtil.getAppCacheSize();
        clear_item_tv.setText(TextUtils.isEmpty(size) || TextUtils.equals("0K", size) ? "无需清理" : size);
    }

}
