package com.rehome.sgbaxj.activity.sbxdj;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.WindowManager;
import android.widget.GridView;
import com.rehome.sgbaxj.DBModel.Djjh;
import com.rehome.sgbaxj.DBModel.DjjhList;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.activity.yhpc.YhpcDataListActivity;
import com.rehome.sgbaxj.adapter.GridViewAdapter;
import com.rehome.sgbaxj.base.BaseActivity;
import com.rehome.sgbaxj.bean.GridViewBean;
import com.rehome.sgbaxj.bean.XscbRequestBean;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.GsonUtils;
import com.rehome.sgbaxj.utils.HttpListener;
import com.rehome.sgbaxj.utils.NohttpUtils;
import com.rehome.sgbaxj.utils.SPUtils;
import com.rehome.sgbaxj.utils.StatusBarUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ruihong on 2017/11/24.
 */

public class MainSbxdjglActivity extends BaseActivity {

    GridView gv;
    private String[] str = {"点检任务", "点检工作", "隐患排查上传", "隐患整改上传"};
    private int[] imageId = {R.mipmap.icon3, R.mipmap.icon6, R.mipmap.icon_dxj, R.mipmap.icon_dxj};
    private int[] colors = {R.drawable.radius_b1, R.drawable.radius_b2, R.drawable.radius_b1, R.drawable.radius_b1};
    private GridViewAdapter adapter;

    @Override
    public int getContentViewID() {
        return R.layout.activity_sbxdjgl;
    }

    protected void initView() {
        gv = findViewById(R.id.gv);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        StatusBarUtil.transparencyBar(MainSbxdjglActivity.this);
    }

    public void initData() {
        mToolbar.setBackgroundColor(Color.parseColor("#00000000"));
        setTitle("设备点检");
        downloadDjData();
        setBack();
        adapter = new GridViewAdapter(this, getGridViewData(), new ArrayList<>(), true);
        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gv.setAdapter(adapter);
        gv.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent;
            switch (i) {
                case 0:
                    intent = new Intent(MainSbxdjglActivity.this, DjMainActivity.class);
                    startActivity(intent);
                    break;
                case 1:
                    intent = new Intent(MainSbxdjglActivity.this, DjgwListActivity.class);
                    startActivity(intent);
                    break;
                case 2:
                    intent = new Intent(MainSbxdjglActivity.this, YhpcDataListActivity.class);
                    startActivity(intent);
                    break;
                case 3:
                    showToast("此功能还在开发中...");
                    break;

            }
        });
    }

    private List<GridViewBean> getGridViewData() {

        List<GridViewBean> datas = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            GridViewBean bean = new GridViewBean();
            bean.setTitle(str[i]);
            bean.setBackgroup(colors[i]);
            bean.setImageid(imageId[i]);
            bean.setShow(false);
            datas.add(bean);
        }
        return datas;
    }

    private void downloadDjData() {
        Request<String> requestdj = NoHttp.createStringRequest(Contans.IP + Contans.DJJHLIST, RequestMethod.POST);
        requestdj.setDefineRequestBodyForJson(createZyJson());
        NohttpUtils.getInstance().add(null, 1, requestdj, callback, false, false, "加载中...");
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {
            if (what == 1) {
//                Log.e("serviceData", response.get());
                DjjhList list = GsonUtils.GsonToBean(response.get(), DjjhList.class);
                if (list != null) {
                    if (list.getState().equals("1")) {
                        int dbcount = -1;//数据库中是否有数据
                        List<Djjh> djjhs = list.getData();//服务器数据
                        //先删除未下载的计划
                        DataSupport.deleteAll(Djjh.class, "download = 0");
                        for (Djjh djjh : djjhs) {
                            List<Djjh> dbdjjh = DataSupport.where("GWID = ? and download = ?", djjh.getGWID(), "1").find(Djjh.class);
                            //如果数据库中没有这条数据，就添加到数据库
                            if (dbdjjh.size() == 0) {
                                dbcount = 1;
                                djjh.save();
                            }
                        }
                        if (dbcount == 1) {
//                                shownotification(1, "您有新的点检计划");
                        }
                    } else {
                        DataSupport.deleteAll(Djjh.class, "download = 0");
                    }
                }
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {
        }
    };

    private String createZyJson() {
        String GWID = (String) SPUtils.get(getApplicationContext(), Contans.BZBH, "");
        String YHID = (String) SPUtils.get(getApplicationContext(), Contans.USERID, "");
        XscbRequestBean info = new XscbRequestBean();
        info.setAction("DJ_GWLIST_GET");
        info.setGWID(GWID);
        info.setYHID(YHID);
        String json = GsonUtils.GsonString(info);
        return json;
    }
}