package com.rehome.bhdxj.activity.sbxj;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.WindowManager;
import android.widget.GridView;
import com.rehome.bhdxj.DBModel.Xjjh;
import com.rehome.bhdxj.DBModel.XjjhList;
import com.rehome.bhdxj.R;
import com.rehome.bhdxj.adapter.GridViewAdapter;
import com.rehome.bhdxj.base.BaseActivity;
import com.rehome.bhdxj.bean.GridViewBean;
import com.rehome.bhdxj.bean.XsRequestInfo;
import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.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;
import static com.rehome.bhdxj.utils.GsonUtils.GsonToBean;

/**
 * 巡视抄表管理
 */
public class XscbglActivity extends BaseActivity {

    GridView gv;

    private String[] str = {"巡检任务", "巡检工作","燃料周期详情"};
    private int[] imageId = {R.mipmap.icon_xs1, R.mipmap.icon6,R.mipmap.icon8};
    private int[] colors = {R.drawable.radius_b1, R.drawable.radius_a4,R.drawable.radius_c1};
    private GridViewAdapter adapter;

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

    @Override
    protected void initView() {

        gv = findViewById(R.id.gv);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        StatusBarUtil.transparencyBar(XscbglActivity.this);
        mToolbar.setBackgroundColor(Color.parseColor("#00000000"));
        setTitle("设备巡检");
        setBack();
        requestDatas();

        adapter = new GridViewAdapter(this, getGridViewData(), new ArrayList<Integer>(), true);
        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gv.setAdapter(adapter);
        gv.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = null;
            switch (i) {
                case 0:
                    intent = new Intent(XscbglActivity.this, XjMainActivity.class);
                    break;
                case 1:
                    intent = new Intent(XscbglActivity.this, SxSbListActivity.class);
                    break;
                case 2:
                    intent = new Intent(XscbglActivity.this, YxrlinfoActivity.class);
                    break;
            }
            if (intent != null) {
                startActivity(intent);
            }
        });
    }

    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 requestDatas() {    //下载巡检数据

        final Request<String> requestxs = NoHttp.createStringRequest(Contans.IP + Contans.XSCB, RequestMethod.POST);

        requestxs.setDefineRequestBodyForJson(createJson());

        NohttpUtils.getInstance().add(this, 0, requestxs, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                XjjhList list = GsonToBean(response.get(), XjjhList.class);

                if (list != null) {

                    if (list.getState().equals("1")) {

                        List<Xjjh> xjjhs = list.getData();//服务器数据

                        DataSupport.deleteAll(Xjjh.class, "download = 0");

                        for (Xjjh xjjh : xjjhs) {

                            List<Xjjh> dbxjjh = DataSupport.where("zxid = ? and download = ? ", xjjh.getZxid(), "1").find(Xjjh.class);

                            if (dbxjjh.size() == 0) {

                                xjjh.save();
                            }
                        }

                    } else {

                        DataSupport.deleteAll(Xjjh.class, "download = 0");
                    }


                }
            }

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

            }
        });
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("XSCB_ZXJL_GET");
        info.setZymc(Contans.YXCB_ZY_ID);
        info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
        String json = GsonUtils.GsonString(info);
        return json;
    }
}
