package com.android.chmo.ui.activity.model;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.Res;
import com.android.chmo.http.service.FindService;
import com.android.chmo.model.ModelZone;
import com.android.chmo.ui.adpater.ShowImageAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.android.chmo.R.id.commentNum;
import static com.android.chmo.R.id.goodNum;

/**
 * Created by Administrator on 2018/8/6.
 */

public class ShowImageActivity extends BaseActivity {
    @BindView(R.id.pager)
    ViewPager pager;

    @BindView(R.id.info)
    TextView infoView;
    @BindView(R.id.good)
    TextView goodView;
    @BindView(goodNum)
    TextView goodNumView;
    @BindView(commentNum)
    TextView commentNumView;

    private List<String> imgList = new ArrayList<>();

    private ModelZone zone;


    @OnClick(R.id.back)
    public void back() {
        close();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_show_images;
    }

    @Override
    public void initView() {
        imgList = getIntent().getStringArrayListExtra("images");
        zone = (ModelZone) getIntent().getSerializableExtra("zone");
        pager.setOffscreenPageLimit(3);
        ShowImageAdapter imagePagerAdapter = new ShowImageAdapter(this,getSupportFragmentManager(), imgList);
        pager.setAdapter(imagePagerAdapter);

        infoView.setText(zone.intro);
        fillView();
    }

    private void fillView() {
        goodView.setText(zone.islikes == 1 ? "取消" : "赞");
        if(zone.likeCount == 0 && zone.privateinfo != null) {
            zone.likeCount = zone.privateinfo.size();
        }
        goodNumView.setText(zone.likeCount + "");

        if(zone.commentCount == 0 && zone.recordinfo != null) {
            zone.commentCount = zone.recordinfo.size();
        }
        commentNumView.setText(zone.commentCount+"");
    }

    @OnClick(R.id.good)
    public void good() {
        showLoading();
        final int type = zone.islikes == 1 ? 2: 1;
        FindService.setGood(zone.privatepk,type, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                Res res = new Gson().fromJson(result,Res.class);
                if(res.msg.equals("success")) {
                    zone.islikes = type;
                    if(type == 2) {
                        zone.likeCount--;
                    }else {
                        zone.likeCount++;
                    }
                    goodView.setText(zone.islikes == 1 ? "取消" : "赞");
                    goodNumView.setText(zone.likeCount + "");
                    EventBus.getDefault().post("zoneRefresh");
                }else {
                    showToast("操作失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("操作失败");
            }
        });
    }

    @OnClick({R.id.comment,R.id.count_content})
    public void comment(View v) {
        Intent intent = new Intent(this,ModelZoneDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("zone",zone);
        openPageForResult(intent, 1);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 1) {
            if(data.hasExtra("zone")) {
                this.zone = (ModelZone)data.getSerializableExtra("zone");
                fillView();
            }
        }
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("zone", zone);
        setResult(RESULT_OK, data);
        super.finish();
    }
}
