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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.model.BannerInfo;
import com.android.chmo.ui.view.LongImageView;
import com.android.chmo.ui.view.TopBar;
import com.android.chmo.utils.ImageUtils;

import java.io.InputStream;
import java.net.URL;

import butterknife.BindView;

import static com.android.chmo.R.id.pic;

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

public class BannerInfoActivity extends BaseActivity {
    @BindView(R.id.topBar)
    TopBar topBar;

    @BindView(pic)
    LongImageView imageView;

    private BannerInfo banner;

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

    @Override
    public void initView() {
        banner = (BannerInfo) getIntent().getSerializableExtra("banner");

        topBar.setTitle(banner.caption);

        loadImage();
    }


    private void loadImage() {

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    showToast("加载失败");
                }else {
                    Bitmap bitmap = (Bitmap)msg.obj;
                    imageView.setImage(bitmap);
                }
                hideLoading();
            }
        };
        showLoading();
        new Thread() {
            @Override
            public void run() {
                try {
                    String fileName = getFileName();

                    Bitmap bitmap = ImageUtils.getBitmap(getContext(), fileName);
                    if(null != bitmap) {
                        Message msg = handler.obtainMessage(1);
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                    }else {
                        URL url = new URL(HttpApi.getImgUrl(banner.pic));
                        InputStream inputStream=url.openStream();
                        bitmap = BitmapFactory.decodeStream(inputStream);
                        Message msg = handler.obtainMessage(1);
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                        inputStream.close();

                        ImageUtils.saveImage(getContext(),url.openStream(), fileName);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(0);
                }catch (OutOfMemoryError e) {
                    System.gc();
                    System.runFinalization();
                    handler.sendEmptyMessage(0);
                }

            }
        }.start();
    }


    private String getFileName() {
        if(TextUtils.isEmpty(banner.pic)) {
            return "";
        }

        String fileName = banner.pic.substring(banner.pic.lastIndexOf("/")+1);
        return fileName;
    }
}
