package cn.net.caoyao.cpzshandset.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.adapter.PiCiListAdapter;
import cn.net.caoyao.cpzshandset.bean.PiCiBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.service.presenter.LoginApis;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class PiCiListActivity extends BaseActivity {
    RecyclerView rv_picilist;
    @Override
    public int loadView() {
        return R.layout.activity_pi_ci_list;
    }

    @Override
    public void initTitleView() {
        View image_title_back_common = findViewById(R.id.image_title_back_common);
        image_title_back_common.setVisibility(View.VISIBLE);
        image_title_back_common.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        image_title_back_common.setOnClickListener(this);
       TextView   tv_title_common = (TextView) findViewById(R.id.tv_title_common);
        tv_title_common.setText("选择批次号");
    }

    @Override
    public void initView() {
        rv_picilist = (RecyclerView)findViewById(R.id.rv_picilist);
        LinearLayoutManager manager = new LinearLayoutManager(PiCiListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_picilist.setLayoutManager(manager);

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        final long productId = intent.getLongExtra("productId", 0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                getPiCiList(productId);
            }
        }).start();

    }
    public void getPiCiList(long id){
        SharedPreferences user_token =getSharedPreferences("data", 0);
        String token = user_token.getString("appToken", "");

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.caoyaoUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        LoginApis loginApis = retrofit.create(LoginApis.class);
        Observable<ResponseBody> observable=loginApis.getPiCiList("Bearer  "+token,id);
        observable.subscribeOn(Schedulers.io());
        observable.observeOn(AndroidSchedulers.mainThread());
        observable.subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(ResponseBody value) {
                try {
                    String string = value.string();
                    Gson gson = new Gson();
                    PiCiBean piCiBean = gson.fromJson(string, PiCiBean.class);
                    int code = piCiBean.getCode();
                    if (code==200){
                        List<PiCiBean.DataBean> data = piCiBean.getData();
                        Message message=new Message();
                        message.obj=data;
                        message.what=1;
                        handler.sendMessage(message);
                    }else {
                        Message message=new Message();
                        message.obj=piCiBean.getMsg();
                        message.what=2;
                        handler.sendMessage(message);
                    }
                } catch (IOException e) {
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    final List<PiCiBean.DataBean> list=(List<PiCiBean.DataBean>)msg.obj;
                    PiCiListAdapter piCiListAdapter = new PiCiListAdapter(list, PiCiListActivity.this);
                    rv_picilist.setAdapter(piCiListAdapter);
                    piCiListAdapter.onClick(new PiCiListAdapter.JieKou() {
                        @Override
                        public void onClick(int pos) {
                            PiCiBean.DataBean dataBean=new PiCiBean.DataBean();
                            dataBean.setId(list.get(pos).getId());
                            dataBean.setProductBatchType(list.get(pos).getProductBatchType());
                            dataBean.setProductBatchCode(list.get(pos).getProductBatchCode());
                            Intent intent = new Intent();
                            intent.putExtra("choicedPiCi", dataBean);
                            setResult(1010,intent);
                            finish();
                        }
                    });
                    break;
                case 2:
                    String str=(String) msg.obj;
                    Toast.makeText(PiCiListActivity.this,str,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
}