package com.longtu.sample;


import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.acegameproject.burstshooter.R;

import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.LTBaseDataCollector;
import com.longtu.sdk.base.share.LTBaseShareLogin;
import com.longtu.sdk.base.share.LTShareCallback;
import com.longtu.sdk.base.share.LTShareParams;
import com.longtu.sdk.base.utils.LTSDKUtils;
import com.longtu.sdk.utils.Log.Logs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Demo 分享控件， 实现SDK分享功能和接口调用。
 */
public class LTBase_Demo_ShareView extends LinearLayout implements OnClickListener {
    public final static String LOGTAG = "ltbase_demo";
    public static final int Image_requestCode = 11223453;
    private Context mContext;

    private int type = 0;
    private String Flag = "";


    /**
     * 缓存test
     * 存放一张图片到缓存
     */
    private Button mImageCacheTest;

    /**
     * 一键分享,不需要指定分享渠道
     * 测试
     */
    private Button mOnekey, mOnekeyImage, mOnekeyUrl;

    /**
     * 指定渠道分享
     * 测试
     */
    private Button mWeiboTetx, mWeiboTetxImg, mWeiboLink, mQQimage, mQQimagetext, mQQlink, mQzonetext, mQzoneimage,
            mQzonelink, mWxfriendtext, mWxfriendimage, mWxfriendlink, mWxtimelinetext, mWxtimelineimage,
            mWxtimelinelink;

    public LTBase_Demo_ShareView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.longtu_demo_sharelayout, this);


        mImageCacheTest = (Button) v.findViewById(R.id.share_imagecache);
        mOnekey = (Button) v.findViewById(R.id.share_onekey);
        mOnekeyImage = (Button) v.findViewById(R.id.share_onekeyimage);
        mOnekeyUrl = (Button) v.findViewById(R.id.share_onekeyurl);

        mWeiboTetx = (Button) v.findViewById(R.id.share_weibo_text);
        mWeiboTetxImg = (Button) v.findViewById(R.id.share_weibo_textimg);
        mWeiboLink = (Button) v.findViewById(R.id.share_weibo_link);

        mQQimage = (Button) v.findViewById(R.id.share_QQ_image);
        mQQimagetext = (Button) v.findViewById(R.id.share_QQ_textimg);
        mQQlink = (Button) v.findViewById(R.id.share_QQ_link);

        mQzonetext = (Button) v.findViewById(R.id.share_Qzone_text);
        mQzoneimage = (Button) v.findViewById(R.id.share_Qzone_textimg);
        mQzonelink = (Button) v.findViewById(R.id.share_Qzone_link);

        mWxfriendtext = (Button) v.findViewById(R.id.share_wxfriend_text);
        mWxfriendimage = (Button) v.findViewById(R.id.share_wxfriend_textimg);
        mWxfriendlink = (Button) v.findViewById(R.id.share_wxfriend_link);

        mWxtimelinetext = (Button) v.findViewById(R.id.share_wxtimeline_text);
        mWxtimelineimage = (Button) v.findViewById(R.id.share_wxtimeline_textimg);
        mWxtimelinelink = (Button) v.findViewById(R.id.share_wxtimeline_link);


        mImageCacheTest.setOnClickListener(this);
        mOnekey.setOnClickListener(this);
        mOnekeyImage.setOnClickListener(this);
        mOnekeyUrl.setOnClickListener(this);


        mWeiboTetx.setOnClickListener(this);
        mWeiboTetxImg.setOnClickListener(this);
        mWeiboLink.setOnClickListener(this);

        mQQimage.setOnClickListener(this);
        mQQimagetext.setOnClickListener(this);
        mQQlink.setOnClickListener(this);

        mQzonetext.setOnClickListener(this);
        mQzoneimage.setOnClickListener(this);
        mQzonelink.setOnClickListener(this);

        mWxfriendtext.setOnClickListener(this);
        mWxfriendimage.setOnClickListener(this);
        mWxfriendlink.setOnClickListener(this);

        mWxtimelinetext.setOnClickListener(this);
        mWxtimelineimage.setOnClickListener(this);
        mWxtimelinelink.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == mImageCacheTest) {
            saveImageToCache();
        } else if (v == mOnekey) {

            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Text);
            sharedata.setShareText("我是文本内容分享测试");
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKSendShare(sharedata,
                    new LTShareCallback() {

                        @Override
                        public void LTBaseShareSuccess(int type) {
                            // TODO Auto-generated method stub
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享成功", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void LTBaseShareFail(int type, int code) {
                            // TODO Auto-generated method stub
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    });

        } else if (v == mOnekeyImage) {

            String chachePath = LTSDKUtils.getSDCardPath() + "/ltdemoshare/ltdemo_share1.png";
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_TextImage);
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("我的图片分享内容title");
            sharedata.setShareText("我的图片分享内容");
            sharedata.setShareImagepath(chachePath);
            sharedata.setShareThumbImage(chachePath);

            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKSendShare(sharedata,
                    new LTShareCallback() {

                        @Override
                        public void LTBaseShareSuccess(int type) {
                            // TODO Auto-generated method stub
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享成功", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void LTBaseShareFail(int type, int code) {
                            // TODO Auto-generated method stub
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    });

        } else if (v == mOnekeyUrl) {
            String chachePath = LTSDKUtils.getSDCardPath() + "/ltdemoshare/ltdemo_share1.png";
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Link);
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("龙图游戏");
            sharedata.setShareText("龙图游戏官网链接");
            sharedata.setShareImagepath(chachePath);
            LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKSendShare(sharedata,
                    new LTShareCallback() {

                        @Override
                        public void LTBaseShareSuccess(int type) {
                            // TODO Auto-generated method stub
                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享成功", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void LTBaseShareFail(int type, int code) {
                            // TODO Auto-generated method stub
                            Logs.fi("ltdemo", "分享失败，日志，移除toast后 查看分享状态信息使用");
//                            Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    });
        } else if (v == mWeiboTetx) {

            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Text);
            sharedata.setShareText("1234567890");
            ourshareshend(LTShareParams.Share_Type_SinaWeiBo, sharedata);
        } else if (v == mWeiboTetxImg) {
            type = LTShareParams.Share_Type_SinaWeiBo;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);
        } else if (v == mWeiboLink) {

            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Link);
            sharedata.setShareText("1234567890");
            sharedata.setShareUrl("http://www.longtugame.com");
            ourshareshend(LTShareParams.Share_Type_SinaWeiBo, sharedata);
        } else if (v == mWxfriendtext) {
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Text);
            sharedata.setShareText("1234567890");
            ourshareshend(LTShareParams.Share_Type_WeChat, sharedata);

        } else if (v == mWxfriendimage) {
            type = LTShareParams.Share_Type_WeChat;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);

        } else if (v == mWxfriendlink) {
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Link);
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("龙图游戏");
            sharedata.setShareText("龙图游戏官网链接");
            ourshareshend(LTShareParams.Share_Type_WeChat, sharedata);

        } else if (v == mWxtimelinetext) {

            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Text);

            sharedata.setShareText("1234567890");
            ourshareshend(LTShareParams.Share_Type_WeChatLine, sharedata);

        } else if (v == mWxtimelineimage) {
            type = LTShareParams.Share_Type_WeChatLine;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);

        } else if (v == mWxtimelinelink) {
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Link);
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("龙图游戏");
            sharedata.setShareText("龙图游戏官网链接");
            ourshareshend(LTShareParams.Share_Type_WeChatLine, sharedata);
        } else if (v == mQQimage) {


            type = LTShareParams.Share_Type_QQ;
            Flag = LTShareParams.Share_Flag_Text;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);

        } else if (v == mQQimagetext) {

            type = LTShareParams.Share_Type_QQ;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);

        } else if (v == mQQlink) {
            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_TextImage);
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("龙图游戏");
            sharedata.setShareText("龙图游戏官网链接");
            ourshareshend(LTShareParams.Share_Type_QQ, sharedata);
        } else if (v == mQzonetext) {

            LTShareParams sharedata = new LTShareParams();
            sharedata.setShareFlag(LTShareParams.Share_Flag_Text);
            sharedata.setShareText("文本分享");
            sharedata.setShareUrl("http://www.longtugame.com");
            sharedata.setShareTitle("龙图游戏");
            ourshareshend(LTShareParams.Share_Type_Qzone, sharedata);

        } else if (v == mQzoneimage) {

            type = LTShareParams.Share_Type_Qzone;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);

        } else if (v == mQzonelink) {
            type = LTShareParams.Share_Type_Qzone;
            Flag = LTShareParams.Share_Flag_TextImage;
            // 图片选择器
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            LTBase_Demo_MainActivity.mMainActivity.startActivityForResult(intent, Image_requestCode);
        }

    }

    /**
     * 保存一张默认图片到缓存内，用于测试 没有读取权限的时候分享图片
     */
    private void saveImageToCache() {

        String chachePath = LTSDKUtils.getSDCardPath() + "/ltdemoshare/ltdemo_share1.png";
        Logs.i(LTBaseShareLogin.Log_Tag, " chachePath = " + chachePath);
        byte[] ltdemo_share = getFile_Assets("ltdemo_ltshare1.png");
        Logs.i(LTBaseShareLogin.Log_Tag, " ltdemo_share = " + ltdemo_share);
        saveImageToCache(chachePath, ltdemo_share);


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        Logs.i(LOGTAG, "onKeyDown  keyCode = " + keyCode + " event getRepeatCount = " + event.getRepeatCount());
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // (SDK接口)

            LTBase_Demo_MainActivity.mMainActivity.setContentView(LTBase_Demo_MainActivity.mMainActivity.mPayView);
            LTBase_Demo_MainActivity.mMainActivity.mPayView.mShareView = null;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void share_image(String path) {
        Logs.i(LOGTAG, " share_image path = " + path);
        LTShareParams sharedata = new LTShareParams();
        if (Flag.equals(LTShareParams.Share_Flag_TextImage)) {
            sharedata.setShareFlag(Flag);
            sharedata.setShareText("1234567890");

            if (type != LTShareParams.Share_Type_QQ || type != LTShareParams.Share_Type_Qzone) {
                sharedata.setShareUrl("http://www.longtugame.com");
            }

            sharedata.setShareImagepath(path);
            sharedata.setShareTitle("龙图游戏");
            sharedata.setShareText("龙图游戏官网链接");
        } else {
            // qq纯图分享
            sharedata.setShareFlag(Flag);
            sharedata.setShareImagepath(path);
        }

        ourshareshend(type, sharedata);
    }

    private void ourshareshend(int type, LTShareParams logValue) {
        LTBaseSDK.getInstance(LTBase_Demo_MainActivity.mMainActivity).LTBaseSDKSendShare(type, logValue,
                new LTShareCallback() {

                    @Override
                    public void LTBaseShareSuccess(int type) {
                        // TODO Auto-generated method stub
                        Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void LTBaseShareFail(int type, int code) {
                        // TODO Auto-generated method stub
                        Toast.makeText(LTBase_Demo_MainActivity.mMainActivity, "分享失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }


    public byte[] getFile_Assets(String filename) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream in = LTBaseDataCollector.getInstance().getmActivity().getAssets().open(
                    filename);
            int l = -1;
            byte[] tmp = new byte[64];
            while ((l = in.read(tmp)) > -1) {
                bos.write(tmp, 0, l);
            }

            if (bos.size() > 0) {
                return bos.toByteArray();
            }
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return null;
    }


    /**
     * 保存一张图片到缓存中
     *
     * @param fileName
     * @param content
     */
    public void saveImageToCache(String fileName, byte[] content) {
        try {

            File saveFile = new File(fileName);
            if (!saveFile.exists()) {
                File dir = new File(saveFile.getParent());
                dir.mkdirs();
                saveFile.createNewFile();
            }

            FileOutputStream outStream = new FileOutputStream(saveFile);
            outStream.write(content);
            outStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
