package ai.pai.client.views;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.common.utils.TimeUtils;
import com.android.views.popup.BasePopupWindow;

import ai.pai.client.R;
import ai.pai.client.db.Preferences;
import ai.pai.client.utils.PhotoLoader;


public class GatherInfoPopup extends BasePopupWindow {

    private View popupView;
    private ImageView gatherMap;
    private TextView gatherInfoText;
    private Activity context;


    public GatherInfoPopup(Activity context) {
        super(context);
        this.context = context;
        init();
    }

    @Override
    protected Animation getShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    protected View getClickToDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.pop_gather_info, null);
        gatherInfoText = (TextView) popupView.findViewById(R.id.tv_gather_info);
        gatherMap = (ImageView) popupView.findViewById(R.id.iv_gather_map);
        return popupView;
    }

    private void init() {
        String time = TimeUtils.getGatherTimeStr(Preferences.getInstance(context).getGatherTime() - 30 * 60 * 1000);
        if (time.length() == 0) {
            time = context.getString(R.string.gather_no_time);
        }
        String location = Preferences.getInstance(context).getTourGatherLocation();
        if (location.length() == 0) {
            location = context.getString(R.string.gather_no_location);
        }
        gatherInfoText.setText(context.getString(R.string.gather_info, time, location));
        if (!TextUtils.isEmpty(Preferences.getInstance(context).getGatherMap())) {
            PhotoLoader.getInstance(context).displayImage(Preferences.getInstance(context).getGatherMap(), gatherMap);
            gatherMap.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String lat = Preferences.getInstance(context).getTourGatherLat();
                    String lon = Preferences.getInstance(context).getTourGatherLon();
                    if (TextUtils.isEmpty(lat) || TextUtils.isEmpty(lon) || !callThirdPartyMap(lat, lon)) {
                        new FullScreenImgPopup(context, Preferences.getInstance(context).getGatherMap()).showPopupWindow();
                    }
                }
            });
        } else {
            gatherMap.setVisibility(View.GONE);
        }
    }

    private boolean callThirdPartyMap(String lat, String lon) {

        if (isAppExist("com.autonavi.minimap")) {
            try {
                Intent intent = Intent.getIntent("androidamap://viewMap?sourceApplication=paiai&lat=" + lat + "&lon=" + lon + "&dev=0");
                context.startActivity(intent);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (isAppExist("com.baidu.BaiduMap")) {
            double latd = Double.parseDouble(lat);
            double lond = Double.parseDouble(lon);
            Double[] loc = GCJ02ToBD09(latd, lond);
            latd = loc[0];
            lond = loc[1];
            try {
                Intent intent = Intent.getIntent("intent://map/marker?location=" + String.valueOf(latd) + "," + String.valueOf(lond) + "&title=集合位置&src=paiai#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                context.startActivity(intent);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    private boolean isAppExist(String packageName) {
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    private static double x_PI = 3.14159265358979324 * 3000.0 / 180.0;

    public static Double[] GCJ02ToBD09(Double gcj_lon, Double gcj_lat) {
        double z =
                Math.sqrt(gcj_lon * gcj_lon + gcj_lat * gcj_lat) + 0.00002 * Math.sin(gcj_lat * x_PI);
        double theta = Math.atan2(gcj_lat, gcj_lon) + 0.000003 * Math.cos(gcj_lon * x_PI);
        Double[] arr = new Double[2];
        arr[0] = z * Math.cos(theta) + 0.0065;
        arr[1] = z * Math.sin(theta) + 0.006;
        return arr;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }


}
